Web Server on Node JS

 

     In the previous articles we saw various features of NodeJS. Now it is time to sew everything together and start making our Web Server. Before we start building we should know what role does URL play in server.

 For this you should know what are the different parts of a URL:
The domain is a human readable address of the web page. It is translated to the actual IP address of the server by various DNS. The route of the URL is what decides what to execute on the server. Query and Params are some data which we send via the URL.

Simple Web Server
To create a basic web server, we will use the http module. This is one of a core NodeJS modules used for building a web server. It lets you interface the routes and params of a request to execute functions accordingly. 

Create a server.js file with the following code:

server.js

use strict;
const http = require(http);
let PORT = 3000;

let server = http.createServer( function(req, res) {
    res.end(You have visited +req.url);
});

server.listen( PORT, function(){
    console.log(Server started listening on port: +PORT);
});

Now run this code, you should see something like this:


Note that the script does not exit and keeps running. To see the server in action, open your favorite web browser and visit http://localhost:3000 or http://127.0.0.1:3000 depending on how your system is configured. You should see something like this:

Now let us understand the code.
First we require the http module to use it in our application. We then define a PORT on which the server should listen to. Port is a communication endpoint in any operating system. Various processes interact with each other using ports. When developing your web servers, try to use ports greater than 1024 as port numbers lesser than those are reserved by the operating system.

Then we create a HTTP server using http.createServer() which accepts a callback function that acts as the handler for our server. All the HTTP requests sent to our server will be handled by this callback function. The logic inside this handler function will be executed on every request. 

The server.listen() function takes two arguments -- PORT number and callback function. The callback function is executed when the process is started and is listening on the specified PORT. It will be executed only once in the process lifetime i.e. until the server crashes or is stopped.
Try changing the last part of url

The object req.url returns the route part of the URL and not the complete URL as the domain name depends on the system where the server is hosted. 

To add the some logic to our server modify the handler function as:


let server = http.createServer( function(req, res) {
    if(req.url === /articles){
        res.end(Code to display articles here);
    }else if(req.url === /stats){
        res.end(Stats will be displayed here once we complete our app);
    }else{
        res.end(More logic to be implmented);
    }
});

Restart the server by terminating the node process and again starting it and visit the urls in your browser and visit different routes:

  


This is how a Web Server is built on NodeJS. 

This is all well and good but in a large web application, there will be many routeswith different methods and different logic. If we use http module for such large applications the whole code will become very complicated. In the next article we will see how to use ExpressJS for simplifying the server code.



No comments:
Write comments

Popular Posts