Node.js – Server side JavaScript


June 26, 2011JavascriptNo comments

Node.js the first among the series of posts in “JavaScript weekNode.js a popular JavaScript framework which is used in servers to create scalable web applications.

JS week 1

What is so unique about Node.JS

1.JavaScript normally used in browsers [client side] but node.js puts the JavaScript on server side thus making communication between client and server will happen in same language

2.Servers are normally thread based but Node.JS is “Event” based.Thread based servers like Apache server will serve each web request as individual threads which doesn’t fit well for concurrent connections.Node.JS serves each request in a Evented loop that can able to handle simultaneous requests.

3.Node.JS programs are executed by “V8″ Javascript engine the same used by Google chrome browser.

Event based programming

In a normal process cycle the webserver while processing the request will have to wait for the IO operations like reading the file and writing to disk and thus blocking the next request to be processed.This causes the considerable delay in processing the simultaneous requests. Node.JS process each request as events, The server doesn’t wait for the IO operation to complete while it can handle other request at the same time.When the IO operation of first request is completed it will call-back the server to complete the request.

Here’s a example Node program that  responds with “Hello World” for every request.

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, "");
console.log('Server running at');

Node.JS fits perfect for highly scalable web applications.

You can see the Documention of Node.JS to get started.

Leave a Reply