HTTP SERVER IN NODE.JS

HTTP Server in Node.js

In previous articles, we’ve introduced you to the most basic Node.js program possible. Node.js is more usually known for creating highly scalable server applications. In this article we will introduce you to a simple HTTP server built atop Node.js.

HTTP Server in Node.js

RUNNING THE SERVER

Start by creating a new file named “web_server.js”. Insert the following code into the file and save it.

var http = require(“http”);
var server = http.createServer(function(request, response) {
response.writeHead(200, {“Content-Type”: “text/html”});
response.write(“<!DOCTYPE “html”>”);
response.write(“<html>”);
response.write(“<head>”);
response.write(“<title>Hello World Page</title>”);
response.write(“</head>”);
response.write(“<body>”);
response.write(“Hello World!”);
response.write(“</body>”);
response.write(“</html>”);
response.end();
});
server.listen(80);
console.log(“Server is listening”);

To start the server, type the following command shown below. If everything works well, the user will see a message that the server is listening. Note that the instance the server attempts to bind to port 80, the standard HTTP port. If this port is already in use, or limited on user’s machine, the user will experience an error.

NODE WEB_SERVER.JS

The next step is to connect to the server using a web browser. Launch the browser of choice, and direct it to either of the following links. In networking terms, local host refers to the machine the user is currently using. The browser should be saying “Hello World!”.

http://localhost

http://127.0.0.1

HOW THE SERVER WORKS

The first thing to notice is the call to require() on line 1. Node.js provides a simple module system with a huge developer community. Node.js programs can load specific modules using the require() method. While many modules must be downloaded, some modules, such as http are included with Node.js installations.

Next, the HTTP server is created using the http module’s createServer() method. Comparable to the most Node.js functions, createServer() takes a callback function as an argument. This callback function is implemented each time the server receives a new request.

The callback function takes two arguments, request and response. The request object contains information about the client’s request. In the same way, the response object is used to return data back to the client.

The callback function begins by calling the response.writeHead() method. This method sends an HTTP status code and a collection of response headers back to the client. The status code is used to specify the result of the request.

Along with the status code, the server returns a number of HTTP headers which define the parameters of the response. If the user does not specify headers, Node.js will implicitly send them for the user.

Next, the server executes several calls to response.write(). These calls are used to write the HTML page. By default, UTF-8 character encoding is used. To be precise, all of these calls could be combined into a single call to improve performance.

After the HTML page has been written, the response.end() method is called. By calling this method, the user is telling the server that the response headers and body have been sent, and that the request has been fulfilled.

The call to listen() causes the server to bind to a port and listen for incoming connections. In order to connect to the server, clients must know exactly which port the server is listening on. Ports are identified by port numbers, with HTTP servers typically listening to port 80.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>