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.

Code Explanation:

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. 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. 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. In the response itself, we are sending the string ‘Hello World.’ 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:

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

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: @.

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.

Code Explanation:

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. 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

Error – In case there is any error received when using the GET request, it will be recorded here. Response- The response will have the http headers which are sent back in the response. Body- The body will contain the entire content of the response sent by Google.

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

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.