Create HTTP Web Server in Node.js: Tutorial with Code Example

The Node.js framework is mostly used to create server-based applications. The framework can easily be used to create web servers which can serve content to users.

There are a variety of modules such as the “http” and “request” module, which helps in processing server related requests in the webserver space. We will have a look at how we can create a basic web server application using Node js.

Node as a web server using HTTP

Let’s look at an example of how to create and run our first Node js application.

Our application is going to create a simple server module which will listen on port no 7000. If a request is made through the browser on this port no, then server application will send a ‘Hello World’ response to the client.

Node as a Web Server Using HTTP

Code Explanation

  1. The basic functionality of the require function is that it reads a JavaScript file, executes the file, and then proceeds to return the exports object. So in our case, since we want to use the functionality of the http module, we use the require function to get the desired functions from the http module so that it can be used in our application.
  2. In this line of code, we are creating a server application which is based on a simple function. This function is called whenever a request is made to our server application.
  3. When a request is received, we are saying to send a response with a header type of ‘200.’ This number is the normal response which is sent in an http header when a successful response is sent to the client.
  4. In the response itself, we are sending the string ‘Hello World.’
  5. We are then using the server.listen function to make our server application listen to client requests on port no 7000. You can specify any available port over here.

If the command is executed successfully, the following Output will be shown when you run your code in the browser.

Output

Node as a Web Server Using HTTP

From the output,

  • You can clearly see that if we browse to the URL of localhost on port 7000, you will see the string ‘Hello World’ displayed in the page.
  • Because in our code we have mentioned specifically for the server to listen on port no 7000, we are able to view the output when browsing to this url.

Here is the code for your reference

var http=require('http')
var server=http.createServer((function(request,response)
{
	response.writeHead(200,
	{"Content-Type" : "text/plain"});
	response.end("Hello World\n");
}));
server.listen(7000);

Handling GET Requests in Node.js

Making a GET Request to get the data from another site is relatively very simple in Node.js. To make a Get request in the node, we need to first have the request module installed. This can be done by executing the following line in the command line

npm install request

The above command requests the Node package manager to download the required request modules and install them accordingly.

When your npm module has been installed successfully, the command line will show the installed module name and version: <name>@<version>.

Handling GET Requests

In the above snapshot, you can see that the ‘request’ module along with the version number 2.67.0 was downloaded and installed.

Now let’s see the code which can make use of this ‘request’ command.

Handling GET Requests

Code Explanation

  1. We are using the ‘request’ module which was installed in the last step. This module has the necessary functions which can be used to make GET requests to websites.
  2. We are making a GET Request to www.google.com and subsequently calling a function when a response is received. When a response is received the parameters(error, response, and body) will have the following values
    1. Error – In case there is any error received when using the GET request, it will be recorded here.
    2. Response- The response will have the http headers which are sent back in the response.
    3. Body- The body will contain the entire content of the response sent by Google.
  3. In this, we are just writing the content received in the body parameter to the console.log file. So basically, whatever we get by going to www.google.com will be written to the console.log.

Here is the code for your reference

var request = require("request");
	request("http://www.google.com",function(error,response,body)
	{
		console.log(body);
	});

Summary

  • The Node.js framework can be used to develop web servers using the ‘http’ module. The application can be made to listen on a particular port and send a response to the client whenever a request is made to the application.
  • The ‘request’ module can be used to get information from web sites. The information would contain the entire content of the web page requested from the relevant web site.