Introduction to Node JS
Jul 6, 2012
Suresh Vasudev
4 minute read

Node.js is a server side JavaScript (JS) Framework. Server side scripts are those residing on the server (web server mainly for web applications) and it will be executed at the server before the page gets rendered (like PHP).

It is created using C / C++ and built on V8. V8 is a powerful JS engine created by Google designed for Web. Like JS, the Node.js is also even driver and it has the same syntax as that of JavaScript.

It is created by Ryan Dahl to write better concurrent application. Concurrent applications are those which can do more than one thing at a time. It built in libraries for HTTP, DNS and TCP protocols.

Node.Js is available in UNIX, Linux, Mac and Windows flavor

There is a provision available in Node.js to extend its features by adding third party modules to it like Gems in Ruby. This can be achieved with the help of a process manager – Node Process Manager.

Installing Node

Latest version of Node can be installed from https://github.com/joyent/node/wiki/Installation . An EXE version is available for windows environment.

To make sure the installation is perfect do the following

NJS [1] Open a Command / Terminal window

[2] Type node. It will throw back a prompt “>”

[3] Type 1 + 1 and it will print 2

Now we have successfully installed Node.js

Node.js API documentation

The node documentation and API are available at http://www.nodejs.org/api/

When we learn a new language or framework, we will be eager to know the major features offered by it. I strongly believe that scanning the API documentation will give a good insight of its capability. So we should all learn the art of reading the API documentation of it. This is how I preferred to learn in detail about a new language or framework.

As any language, let us start here also with a Hello World application which will send a “Hello World” string in response to a HTTP request. Let us scan the API documentation to understand the HTTP APIs available in node.js. The HTTP APIs are available at http://nodejs.org/api/http.html

Scanning the HTTP API Documentation

The first method we notice is http.createServer, which will return a Web Server Object (WOW!!!) This method takes a RequestListener as argument but the problem is we cannot find a RequestListener class in the API.

On further scanning the API I realized that reading the API of Node.js is slightly different from that of reading a Java API. I have to read carefully to understand it.

One has to read the description of the method to get a clear picture about its arguments. As per documentation of createServer method, it states that “The requestListener is a function which is automatically added to the ‘request’ event.” This will give us a clue that it is something to do with the request Event.

On Scanning forward we could see the documentation of Event – request. There it is mentioned that “function (request, response) { }” will be invoked each time a request is received. So if we combine createServer and Request Event documentation it is clear that RequestListener is a callback of type - function (request, response) { }

Now we are clear and let us go back and create our first Hello World application.

Hello World Application

This application will be a small web server which will receive a HTTP request and return “Hello World” as response.

[1] Open an Editor and type in the following code.

[javascript] var http = require (‘http’); var server = http.createServer ( function (request, response) { response.writeHead(200); response.end(”

Hello World

”); }); server.listen(3000, “127.0.0.1”); console.log (“The server started and waiting for request at port 3000”); [/javascript]

[2] Save the file as HelloWorld.js

Go to the directory where HelloWorld.js is saved and execute the following command

node HelloWorld.js

Application will respond that Server has started and listening to port 3000

NJS

[3] Now Open a Browser window and issue a http request as “http://localhost:3000

WOW!!! Hello World will be displayed in the page. The small application we have written is now behaving like a HTTP server which can take http request and can respond back.

NJS

Enhancing the Hello World application

Let us modify the above example so that it can take a parameter and echo back. Let us pass a name via HTTP request and the server will respond back as “Hello World {Name }

For that we need to extract the parameter from the request and pass it back in the response. We need to scan the API documentation to find out the relevant APIs for that. We need “url” and “querystring” to extract the request parameter from the query string.

[javascript] var http = require (‘http’); var url = require (‘url’); var qs = require (‘querystring’); var server = http.createServer ( function (request, response) { var query = url.parse(request.url).query var name = qs.parse(query).name; response.writeHead(200); response.end(”

Hello World “+ name +”

” ); }); server.listen(3000, “127.0.0.1”); console.log (“The server started and waiting for request at port 3000”); [/javascript]

Let us start the modified HelloWorld.js and try the http request: http://localhost:3000?name=Ryan

NJS