Nodejs: Creating a Nodejs Server

Creating a Nodejs Server

Today, I am going to demonstrate the way to create a nodejs server where our node js app/page can run. Before creating the server, we just need to know how to run a node page .Its so simple. Ok, let’s do it.

  1. Create a folder anywhere you want; I create on my desktop
  2. Got to the folder and create a file named myApp.js(any name you can set)
  3. open the file myApp.js on the IDE you are using(I am using sublime)
  4. Now write on it simple code like the following:Sample Code
  5. open up cmd and browse to the folder you have created like this:Sample Code

Note: node is the command to run any node js file.

Now you can write any javascript code and run it using node command

Creating NodeJs Server:

Now, we are going to create a nodejs server where a nodejs file can run on web browsers.Given below are the steps to create a node server and run it on a web browsers:

  1. Create a file in the same folder called server.js
  2. write the following code on the server.js file
 //include node js http module to create server
 var http=require("http");
 //creating server and keep it in a variable
 var server=http.createServer(function(request,response){
 response.write("Hello World");
//set the port on which node server will be running
 //print a console message just to check
 console.log("Node server is runing on port 8080");


Now, go to cmd and run the server.js using node server.js command and open a web browser and give localhost:8080 on the url and you will see the result.


Explanation of the code:

Now that the server is up and running, it’s time to analyze the code. The first thing to notice is the call to require() on line 2. Nodejs provides a simple module system with a large developer community. Nodejs programs can load individual modules using the require() method. While many modules must be downloaded, some modules, such as http are included with Nodejs installations.

On line 4, the HTTP server is created using the http module’s createServer() method. Like most Node.js functions, createServer() takes a callback function as an argument. This callback function is executed each time the server receives a new request.If you dont know about what callback function is just go to this link.

The callback function takes two arguments, request and response. The request object contains information regarding the client’s request, such as the URL, HTTP headers, and much more. Similarly, 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 indicate the result of the request. For example, everyone has encountered a 404 error before, indicating that a page could not be found. The example server returns the code 200, which indicates success.

Along with the status code, the returns a number of HTTP headers which define the parameters of the response. If you do not specify headers, Node.js will implicitly send them for you. The example server specifies only the Content-Typeheader. This particular header defines the MIME type of the response. In the case of an HTML response, the MIME type is “text/html”.

After the HTML page has been written, the response.end() method is called. By calling this method, we are telling the server that the response headers and body have been sent, and that the request has been fulfilled. The example server calls end() with no parameters. However, end() can also be called like write(), assuming only one call is needed.

The call to listen() on line 11 causes the server to bind to a port and listen for incoming connections. Computers have thousands of ports, which act as communication end points. 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 8080.

Hope, You understand the tutorials next post will consist of other modules along with http .

Leave a Reply

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