Getting started with Node.js-Day 4

Amitesh Kumar
Web For You
Published in
4 min readJun 26, 2020

Hey everyone . Hope you all are doing well. My last blog on “Getting started with node.js- Day 3“ was a hit. So here I am with the Day 4 summary.

Before starting you can go through my last blogs where I have explained fs module and module exports module.

Today we will talk about “http” module in Node.js.

So what is “http” module in node.js?

  • A simple answer to your question is “Node.js has a built-in module called HTTP, which allows Node.js to transfer data over the Hyper Text Transfer Protocol (HTTP).” or in simple words, we can create our web server using this module.

First of all, we need to require “http” module so we use require function for this purpose. Now comes our main task where we want to make our web server.

The HTTP module can create an HTTP server that listens to server ports and gives a response back to the client. We come to the conclusion that we need to create an HTTP server for this purpose. We use the createServer() method to create an HTTP server.

The above code is for creating our own server using createServer() function. We pass a callback function in this createServer() function with the parameters of requests and response from the server side. Whenever our server is created, the additional functionality we add in this callback function will be done via this callback function when it is created.

We have got our request now we need to respond back. So we respond back by writing res.write() function. We can also use res.writeHead() function where we give parameter as a response code(example:200 which is considered as “ok” when what we requested is perfectly alright) and a content type (example: text/html).

We are not done yet. We need to listen to the server at a certain port on our PC. We do it by using listen() function where we give the port number as the parameter. Mostly we use port number 3000.

As we can see we will get response from server on port 3000(localhost).

I have saved this program in app6.js. So now I will use the command — node app.js in the console under the current working directory and press enter.

Using node app6.js

Our server is created. Let's go to localhost:3000" to if our server is returning us “Hello world from node js.”

This is how our localhost at port 3000 looks like

Here we go! Congratulations! We have created our server and we are getting a response back from it as what we expected.

More on createServer() function:

  • The function passed into the “http.createServer()” has a req argument that represents the request from the client, as an object (http.IncomingMessage object).This object has a property called “url” which holds the part of the url that comes after the domain name. (reference: w3schools.com). So we can also use req.url to check where the response is received and the url.
  • Also if you want to use “https” just put an extra “s” with the http variables you have used in the whole program which makes it look like “https”.

Here I end my blog. Hope you liked it.

This blog is written by Amitesh Kumar.

For more contents on the web you can refer:

Happy Coding!

--

--

Amitesh Kumar
Web For You

A web developer to be…sharing whatever I learn.