Node.js explained

Node.js has been making a lot of noice from last few months. Finally I decided to check what is it and why is it going so much popular. After digging into it and reading a lot about it, I can say that I should have explored it long back. Its interesting. Author Ryan Dahl has done a fantastic job. Let me explain you more about Node.js.

What is Node.js

 Node.js is a Javascript library which lets you run your Javascript code at server side. Its a powerful library which provides extra set of features through Node.js API. Unlike other Javascript libraries like jQuery or YUI, Node.js is not meant to provide you easy DOM manipulation and rich GUI. Its a library which is meant to let you execute your Javascript code in server side environment. Execution uses event based model instead of thread based model. Using Node.js you can write network applications which are scalable and better in terms of performance.

Can you explain?

Ok, lets understand it in a better way. In Javascript, you are aware of callback functions which are executed based on certain events. For example, you provide callback functions to AJAX requests. In other words, you are instructing your code to perform certain actions once the request is completed and response is received from Server. But the code doesn’t need to wait for the response from server. Untill it gets the response from server, it can continue with some other tasks. So basically its an asynchronous pattern. This kind of programming model is called Event Based Model. Node.js uses the same principle to perform tasks at server side in asynchronous manner. Be it a file IO or some database fetch request, everything is executed in asynchronous way so that every IO runs in parallel and server doesn’t need to wait for anything. When an asynchronous request is completed then the response is delivered to server which is transferred to client i.e. browser.

How do you claim Node is better?

Node.js is better than traditional Web Servers because a Web Server uses a thread based model but Node.js uses event loop. Let me explain it more. When a WebServer starts, it can be assumed as a process. Now for every request arriving at it, server creates a new thread to serve the request. Reason of making it multithreaded is – server can serve many clients in parallel. It creates new thread instead of new process because threads are lighter than processes and threads of one process can share address space of the process. So context switching cost is lower for a thread than for a process.

Now, What happens in Node? Node.js is also a process but it doesn’t create different thread for every request or task. Instead, the moment it receives the request, it binds the execution with a certain event and passes it to the System Event Loop. Event loop works by polling some internal or external event provider which generally blocks itself untill the specified event has arrived and then it calls the relevant event handler. The event loop almost always operates asynchronously with the message originator.

Benefits? Yes, it provides you great performance benefits in comparison to a thread based model. Lets take them one by one. 

1) When a process has many threads, lots of CPU cycles are wasted in context switching of threads which is not the case with Node.js. Its single threaded. Imagine a scenario where your server has to accept more than million requests, do you agree that either the server has to be super powerfull or it will go mad while doing context switching resulting in non-acceptable response time. Node.js performance is independent of number of requests received because there are not many threads to switch between.

2) Every thread has some cost involved with it. Be it the context switch overhead or system resources like memory and IO resources. When a thread is in between of an important IO request and gets blocked by other thread, it freezes the required resources with it. Hence multiple threads can result in a system running out of resources. That is not the case with Node.js

3) No Deadlocks possible with Node.js

4) Multiple threads can interfere with each other when sharing hardware resources such as caches or translation lookaside buffers(TLBs).

How do I install it?

You can download Node.js from its github repository and install it. Lets do it step by step.

Install

Open your command prompt or terminal and make sure you have GIT installed. Once you have installed GIT on your system, you can fetch latest source code of Node.js.

git clone git://github.com/ry/node.git

cd node

./configure

make

sudo make install

Alternatively you can download latest source from Node.js github repository and unzip in a folder.

cd /path_to_node

./configure

make

sudo make install

Test

If you are able to check the version of Node using node -v or node -version, it makes sure that you have installed Node.js successfully.

Time to write hello world

Whenever I learn any new language, the very first step is to learn how do I write and run “Hello World”. Lets see how it is done in Node.js

1) Create a Javascript file say “hello.js” (Notice, you are writting a .js file)

2) Copy the below code in the file.

var sys = require("sys");
sys.puts("Hello World!");  

save the file. 

3) run this command.
node hello.js 

It will print ”Hello World” on your console. Bingo!!

A simple Web Server using Node.js

Create a new file called “server.js” and paste the code given below.
var sys = require("sys"),
http = require("http");
 http.createServer(function(request, response) {
response.writeHead(200, {“Content-Type”: “text/html”});
response.write(“Hello World!”);
response.end();
}).listen(8080);

 

Lets see what the code is doing.

1) Its requesting two variables called “sys” and “http” from Node.js API. Think of these varibales as import statement in Java or include statements in C. Related operations are wrapped in single object. You can assume these object as modules which contain all the relevant operations in it. So sys object lets you perform all system related actions like priting on console, requesting for IO. http object lets you perform all the http request and response related actions. More objects can be explored in Node.js API. 

2) Using http object, we are creating an asynchronous server which listens on port 8080. In the callback, we have passed an anonymous function which receives two parameters as request and response. Whenever the server recieves any request, this anonymous function is executed and custom response is returned to the client. 

Run the server using the same command.
node server.js 

If you hit http://localhost:8080 or http://127.0.0.1:8080from your local browser, you will see “Hello World” as the response returned to your browser. 

A file server using Node.js

Create a new file called fileServer.js and paste the code given below.

var sys = require("sys"),
http = require("http"),
url = require("url"),
path = require("path"),
fs = require("fs");

http.createServer(function(request, response) {
var uri = url.parse(request.url).pathname;
var filename = path.join(process.cwd(), uri);
path.exists(filename, function(exists) {
if(!exists) {
response.writeHead(404, {“Content-Type”: “text/plain”});
response.write(“404 Not Foundn”);
response.end();
return;
}
fs.readFile(filename, “binary”, function(err, file) {
if(err) {
response.writeHead(500, {“Content-Type”: “text/plain”});
response.write(err + “n”);
response.end();
return;
}
response.writeHead(200);
response.write(file, “binary”);
response.end();
});
});
}).listen(8080);

This is a file server which is supposed to return required file to client. Lets see what the code does.

1) Retrieve the modules sys, http, url, path and fs which are required to perform necessary operations.

2) Create a server which listens on port 8080

3) Using path module, check if the file exists on the server. It it doesn’t, then return a 404 (Not found) response. If the file is found then read the file in binary format.

4) If any error has occurred during file reading, then send a 500 (Internal server error) response, else send the file content in binary format to the client.

Now notice that this program is handling three tasks asynchronously.

1) Create the server and forget it. The moment any request comes, anonymous function passed will take care of it.

2) Send the request to check if the file exists on the server. When it is done with it’s checking, anonymous function passed will take care of it.

3) If the file was found, then send the request to read the file. Once the file reading is done,  anonymous function passed will take care of it.

Node.js creates these requests, their handlers and leaves them on the system event loop to take care when the required event occures. After creating these requests, Node.js can start taking other requests. Now, compare this pattern with other servers where all three requests are taken care of in a thread based model. Sometimes thread is busy in waiting for an IO or sometime it steals some cycles from CPU for its context switch in or out.

So apparently, you can execute every request asynchronously and that happens in parallel.


Why do I need to code in Javascript?

Node.js has been created to write programs using Javascript because Javascript is the best language to imagine anonymous functions and closures. You do’t get so much of implementation flexibility in any other language. Moreover, you can reuse your client side programs at server side without doing much of the change. Node.js is not created in Javascript. It is just that it lets you write your code in Javascript. Internally it is uses V8 (Google Chrome’s javascript engine) to interpret the code. V8 has been proving itself to be the fastest Javascript engine.


Supported Platforms

As of now Node.js is supported only in Linux, Macintosh and Solaris. For using it on Windows you need to  use some kind of BSD based system  ex. FreeBSD or OpenBSD.

Well, you get the idea now :)

2 thoughts on “Node.js explained

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>