A normal web application works as follows: a browser sends a request to a website. The site’s server receives the request, tracks down the requested file, process the request and sends a response to the browser. In traditional web servers, such as Apache, each request causes the server to create a new system process to handle that request.

There was another improvement came in the form of Ajax. Instead of requesting a whole new page each time, we’d only request the piece of information we actually wanted.

But consider the case of social networking sites, where each user’s feed is updating in real time. The only way that’s possible is if each user has an active connection to the server at all times. The simplest way to do that at present is through long polling.

Long polling essentially tricks HTTP into acting like a persistent connection: as soon as the page loads, it fires off an Ajax request to the server, even if the page doesn’t want anything in particular. But, unlike a normal Ajax request, the server doesn’t respond right away. It just waits, and fires back a response only when it has something new it wants the browser to display. For example, as soon as one of your friends adds a new post, the server returns the response telling the browser to update its display. As soon as the browser receives the response, it shoots back another request. This way the browser is always waiting for a new event to happen on the server side.

Now think about what that means for a traditional web server like Apache. For each and every user connected to the site, your server has to keep a connection open. Each connection requires a process, and each of those processes will spend most of its time either sitting idle (consuming memory) or waiting on a database query to complete. This means that it’s hard to scale up to high numbers of connections without grinding to a near halt and using up all your resources.

Here Node.js comes to play: Two important terms that comes with node.js , specifically non-blocking and event-driven. Think of a non-blocking server as a loop: it just keeps going round and round. A request comes in, the loop grabs it, passes it along to some other process (like a database query), sets up a callback, and keeps going round, ready for the next request.

If the database query comes back , we’ll deal with that the same way: throw a response back to the client and keep looping around. There’s theoretically no limit on how many database queries you can be waiting on, or how many clients have open requests, because you’re not spending any time waiting for them. You deal with them all in their own time, and that’s what event-driven means: the server only reacts when an event occurs. That could be a request, a file being loaded, or a query being executed.

NPM: The Node Package Manager

When discussing Node.js, one thing that definitely should not be omitted is built-in support for package management using the NPM tool that comes by default with every Node.js installation. Most of node.js libraries and applications are published on npm, and many more are added every day.

Let’s look at an example of a global install – let’s say coffee-script. The npm command is simple: npm install coffee-script -g. This will typically install the program and put a symlink to it in /usr/local/bin/. This will then allow you to run the program from the console just like any other CLI tool. In this case, running coffee will now allow you to use the coffee-script REPL.

Where Node.js Should Be Used

Node.js is best suited for real-time applications:online games, collaboration tools, chats, analytics app, or anything where what one user (or robot? or sensor?) does with the application needs to be seen by other users immediately, without a page refresh.Node.js is used in situations where your server-side code need less CPU cycle.Its possible to do pure cloud based development with Node.js.

Let us consider the small and most relevant example of Node.js

Chat is the most typical real-time, multi-user application. Why the chat application is really the sweet-spot example for Node.js is that : it’s a lightweight, high traffic, data-intensive application that runs across distributed devices.

Let’s try to depict how it works.

In the simplest example, we have a single chatroom on our website where people come and can exchange messages in one-to-many (actually all) fashion. For instance, say we have three people on the website all connected to our message board.

On the server-side, we have a simple Express.js application which implements two things: 1) a GET ‘/’ request handler which serves the webpage containing both a message board and a ‘Send’ button to initialize new message input, and 2) a websocket server that listens for new messages emitted by websocket clients.

On the client-side, we have an HTML page with a couple of handlers set up, one for the ‘Send’ button click event, which picks up the input message and sends it down the websocket, and another that listens for new incoming messages on the websockets client (i.e., messages sent by other users, which the server now wants the client to display).

When one of the clients posts a message, here’s what happens:

1.Browser catches the Send button click through a JavaScript handler, picks up the value from the input field (i.e., the message text), and emits a websocket message using the websocket client connected to our server (initialized on web page initialization).

2.Server-side component of the websocket connection receives the message and forwards it to all other connected clients using the broadcast method.

3.All clients receive the new message as a push message via a websockets client-side component running within the web page. They then pick up the message content and update the web page in-place by appending the new message to the board.

Advantages and Disadvantages of Node JS


1.By doing application using this technology we need to know only the Javascript.

2.It is very fast

3.It is scalable

4.Have dozens of libraries and good documentation for their libraries.

5.It have a solid community exists.


1.Lack of native OO(Object Oriented) support

2.Lack of compile time error detection

Example of node js

1.Hello world


The example will be a simple HTTP server that responds to every request with the plain text message “Hello World”.

2.Reading and Writing Files

To manage files in Node, we use the fs module (a core module). We read and write files using the fs.readFile() and fs.writeFile() methods, respectively. I will explain the arguments after the following code:


Save this as files.js. Before you run this script, create a file named test.txt in the same directory as files.js.

Every time the server receives a request, the script reads a number from the file, increments the number, and writes the new number to the file. The fs.readFile() method accepts three arguments: the name of file to read, the expected encoding, and the callback function.

Writing to the file, at least in this case, is much more simple. We don’t need to wait for any results, although you would check for errors in a real application. The fs.writeFile() method accepts the file name and data as arguments. It also accepts third and fourth arguments (both are optional) to specify the encoding and callback function, respectively.


Node.js is an open source, cross-platform runtime environment for server-side and networking applications.

The main idea of Node.js: use non-blocking, event-driven I/O to remain lightweight and efficient in the face of data-intensive real-time applications that run across distributed devices.Node.js is not suitable for CPU-intensive operations.Node is for building fast, scalable network applications, as it’s capable of handling a huge number of simultaneous connections with high throughput, which equates to high scalability.

If your application doesn’t have any CPU intensive computation, you can build it in Javascript top-to-bottom, even down to the database level if you use JSON storage Object DB like MongoDB. This eases development (including hiring) significantly. Node js is better.Any CPU intensive computation will block Node.js responsiveness, so a threaded platform is a better approach.

This article is prepared by: Amesh C.A, Nithinkumar C.N, Rafeek R