Some time back, javascript was limited to the front-end but yet it did its job well. But with Node.js entering the scene, now we could build a complete web application, both front-end and the back-end using Javascript.

Yes. You can now have javascript both on frontend and backend for Node.js is "an event-driven I/O server-side javascript environment" as per their official website. Here, we will be going through the steps to create a simple web server using node.js. Assuming that you have setup the node.js development environment, lets begin.

First I would initialize npm (Node Package Manager) within our project folder using the command npm init. This will ask you a bunch of questions like the name of the application, the version, a description, keywords… etc and then write a package.json for you.

Now that we have npm and package.json file ready, lets start coding.

First we have to import the 'http' module which comes with node by default:

var http = require('http');

Then I invoke the method, createServer in the http modules which uses a callback function with parameters for the request and the result:

http.createServer(function(request, result) {
  result.writeHead(200, {'Content-Type': 'text/plain'});
  result.end("Hello World\n");
}).listen(9999, '127.0.0.1');

console.log("Server running at http://127.0.0.1:9999/");

Within the body, I set the header of result a value of 200 and the content type as 'text/plain' for we are going to display a simple string. Next the value to be returned is set through the end method. Then we specify the location and the port address for the server to be running on listening for requests.

To run our server, navigate into the working directory and then give the command npm start to execute our code.