I was trying to look into Node.js which is the new phenomenon on the web these days. Lot of people (websites) are moving to Node - from ruby / java / php. Surely, I want to find out why…

Node Logo

I have followed Node since it’s beginning but, never really got to use it anywhere or had a chance to seriously experiment on it.

Node is effectively a new programming runtime (platform) for just about anything. The language in play is Javascript. It has various modules like ‘http’ which makes it highly useful for development of web services / apps. The runtime is very fast as it’s the V8 engine of Chrome browser. Thanks to Open source community and Google for this.

Node started with Rayn Dahl at Joyent, but now it is in the hands of a foundation. The foundation consists of members from various areas of the web. They are in charge of steering the development of node. This is good as opposed to a normal open source developer model, as everyone recognises a potential in this.

Javascript is best suited for the web, as all the client side code is javascript anyway. This makes it easy for the development of web apps as both the client and server codebase becomes javascript. Smaller technology stack. Or is it?

Javascript is a neat little programming language, which is extremely malleable. It can be moulded for any paradigm viz. it is a functional language but can be turned into an Object Oriented Language. On the other hand, this also makes it a source of a lot of problems and bugs. It is highly recommended to do production node js work using Test Driven Development.

I have been experimenting with the Chrome engine since it began, was really excited by its speed… This was back in 2011

Bezier demo

Bezier demo

Lets have a look a how a node application works. I have downloaded node js from the nodejs website. I am planning to create just a simple hello world program that displays the current system time.

Create a file named app.js (it could be anything) with the following content

var http = require('http'); 

var server = http.createServer(function(request, response) {
    response.writeHead(200, {'Content-Type': 'text/html'})
    var d = new Date();
    var hrs = d.getHours();
    var min = d.getMinutes();
    var sec = d.getSeconds();

    var htmlContent = "<h1>Hello World!</h1><p>Time is: " + 
    hrs + ":" + min + ":" + sec;
    response.end(htmlContent);
});

server.listen(3000);

From the command line start this web service which we have written in the command line

> node app.js

After launching the browser with the http://localhost:3000 we should see the below screen

Output]

The difference between this and something like ruby / java or an ASP.net web application or a service is that this is a self contained - self hosted application. There is no other container requried to host this functionality. For example we have to host the ASP.net website into IIS, ruby into apache, java using jboss etc… Here in node we just have a ‘server.listen(3000)’ line. This is extremely powerful but it is also very low level. To write business applications on top of this requires considerable amount of effort. For example how do we handle routing serve static pages or resources, handle security, handle parameters…? Which has given rise to many web frameworks on the nodejs platform. One of them which seems to be quite popular is Express. More on this soon.

First impressions really are that nodejs is a growing new platform with a big user and developer base, it is actively supported, with a lot of content online. It is very lightweight and powerful runtime, which allows easy deployment also, node has got one of the best package management solutions called the node package manager (npm). Node presents a very low level js platform which gives a huge amount of power and control to the developers however, that means there has to be a reliance on middleware frameworks which will be needed to do the heavy lifting of the business applications. Thankfully there are many such frameworks now :-)