In this partition, we will learn how to create a easy Node.js web server and handle HTTP requests.

To access the web pages of any web application, you requirement a web server. The web server will handle all http appeal for the web application eg IIS web server for ASP.NET applications and Apache web server for PHP or web applications Java.

Node.js provides the ability to create your own web server that will handle HTTP requests harmoniously. You can use IIS or Apache to use the Node.js web application but it is recommended to use the Node.js web server.

Create Node.js Web Server

Node.js makes it easy to create a simple web server that handles incomparably incoming applications.

The following example is a simple Node.js server contained in a server file.

Example

var http = require('http'); // 1 - Import Node.js core module

var server = http.createServer(function (req, res) {   // 2 - creating server

    //handle incomming requests here..

});

server.listen(5000); //3 - listen for any incoming requests

console.log('Node.js web server at port 5000 is running..')

In the example above, we import the http module using the require() function. The http module is the basis of Node.js, so there is no need to install it using NPM. The next step is to call the createServer() http path and specify the task to call the application and response parameter. Finally, call the listen() method of the recovered server object from createServer() route with port number, to start listening to incoming requests in port 5000. You can specify any unused port here.

Launch the web server above by typing the node server.js command into the Prompt command or end window and it will display a message as shown below.

C:\> node server.js
Node.js web server at port 5000 is running..

This is how you build a Node.js web server using simple steps. Now, let's see how we can handle HTTP request and send feedback to Node.js web server.

Manage HTTP request

The http.createServer() method includes the application and response parameters provided by Node.js. The application object can be used to get information about the current HTTP request e.g., url, title and data. The response object can be used to send a response to the current HTTP request.

The below example demonstrates handling HTTP request and response in Node.js.

var http = require('http'); // Import Node.js core module

var server = http.createServer(function (req, res) {   //create web server
    if (req.url == '/') { //check the URL of the current request
        
        // set response header
        res.writeHead(200, { 'Content-Type': 'text/html' }); 
        
        // set response content    
        res.write('

This is home Page.

'); res.end(); } else if (req.url == "/student") { res.writeHead(200, { 'Content-Type': 'text/html' }); res.write('

This is student Page.

'); res.end(); } else if (req.url == "/admin") { res.writeHead(200, { 'Content-Type': 'text/html' }); res.write('

This is admin Page.

'); res.end(); } else res.end('Invalid Request!'); }); server.listen(5000); //6 - listen for any incoming requests console.log('Node.js web server at port 5000 is running..')

In the example above, req.url is used to check the url of the current request and is based on what you are sending feedback. To send a reply, first set a reply header using the writeHead() method and then write the string as the reply body using the write() method. Finally, the Node.js web server sends the answer using the end method ().

Now, run the above web server as shown following.

C:\> node server.js
Node.js web server at port 5000 is running..

To test it, you can use the command-line program curl, which most Mac and Linux machines have pre-installed.

curl -i http://localhost:5000