Express for Node.js

Express is a simple web application framework for building hybrid web applications. You can render web pages, generate  customized responses and create user-friendly APIs quickly with ease. It just fits the trade with Node.js.

This post covers the installation and hello world example:


buntu@ubuntu:~$ sudo npm install express
express@3.1.0 node_modules/express
├── methods@0.0.1
├── fresh@0.1.0
├── cookie-signature@0.0.1
├── range-parser@0.0.4
├── buffer-crc32@0.1.1
├── cookie@0.0.5
├── debug@0.7.2
├── commander@0.6.1
├── mkdirp@0.3.3
├── send@0.1.0 (mime@1.2.6)
└── connect@2.7.2 (pause@0.0.1, bytes@0.1.0, formidable@1.0.11, qs@0.5.1)

Hello World Example:

In the above example:

  • 1. express module is imported and application object is created
  • 2. Object’s get() method is called and callback is set to respond with ‘hello world’
  • 3. When user sends a GET request at port 3000, an event is generated that is handled by callback function(request, response)

Installing Node.js on Ubuntu

I would continue explaining more about Node.js, but for all those folks who do things and understand them, here’s the way to install Node.js on Ubuntu machines..

You need to first install necessary packages like git-core and libssl-dev

sudo apt-get update
sudo apt-get install g++ curl libssl-dev apache2-utils
sudo apt-get install git-core

Download the latest source and build it

gunzip node-v0.8.2.tar.gz
tar xvf node-v0.8.2.tar.gz
cd node-v0.8.2
sudo make install

Check the version after install

node -v


Node.js Async

In my previous post I discussed how asynchronous (with callbacks) nature of Node.js is useful to develop non-blocking server side implementations. Let’s now see how async functions are developed with an example.

Async functions are typically developed with a single callback with result arguments and an optional error.

Here’s an example code:

Now if you run the above code as: node async.js the output is:

 Out of async
 value is:6.082818640342675e+62

Let’s explain what we did here:

We first define a function async_factorial with a callback. Inside async_factorial we do something interesting; we use process.nextTick. We know node runs an event loop and handle i/o operations asynchronously with the help of callback invocations. With process.nextTick, you register the supplied function or piece of code to be executed during the next iteration of event loop. Typically a queue of functions is maintained. So whenever the event loop is free from I/O events, it starts with the next tick or next loop, it then first checks for the queue and executes the functions in the queue and then checks for I/O events and continues the loop. Thus if you need to perform cpu intensive operation and at the same time don’t want to block any I/O event, you can use this trick to perform cpu operation in async way with callbacks using nextTick. This ensures that event loop is free and handle I/O as well. In this case, we process factorial(50) in async way with nextTick.

Execution flow:

1. On calling async_factorial the flow control reaches step1.

2. With process.nextTick we expect, calculation of factorial(50) happens in the next iteration event loop and only would the callback(fact) gets called.

3. Since async_factorial is waiting on callback for result, the next statement ‘Out of asyc’ gets executed in this iteration of event loop.

4. When the next event loop begins, the flow goes to step2, calculates factorial and returns the factorial value through callback

5. Since the result is now available the code inside async_factorial gets executed and we see step3 and value is:6.082818640342675e+62 being printed out.

Note: You could also use setTimeout instead of nextTick that provides you the option of setting the time to wait before performing operation. But as the documentation says, nextTick is more efficient.

Understanding node.js

Essentially Node has an approach of making I/O operations non-blocking. All the I/O operations run independently and generate an event when they are done.  Node follows an execution model of having an event loop (a concept existing in JavaScript) that handles events that are generated by I/O operations ( like database, HTTP requests, file I/O s).  Whenever an I/O operation occurs, the event loop handles the event and processes it by converting it into callback invocation. With this mechanism, the I/O operation is handled in the background and the callback gets executed when the required data is available. This is also called as  asynchronous way of handling events.

Node expects that there is no fixed time within which the I/O is performed. So instead of waiting for the result, Node generates an event (which are in-turn handled by callbacks) and hence the tasks get performed in “parallel” (nothing to do with multi-core and true parallelism;  in fact Node can’t take advantage of multi-core as its a single threaded event loop).

In the hello world example, we have a HTTP server listening on port 8888. When the server receives an I/O activity (http GET request) an event (request) is fired which is tied to callback (anonymous function(request,response) ) which in turn prints ‘Hello World’. Node uses Linux libraries like epoll, select (as does Python Tornado) to get notified that a user has made a connection with the server.

Let’s understand the concept with an example of database call being made in the web server.

var result = mysql.execute(query);
console.log("I am done");

The above code is synchronous in nature. If a database call takes 15 secs to run, HTTP server is blocked. One db request like above can block the web server and as a result any other following requests can’t be served until the first request is complete.

Solution? Let’s try asynchronous then:

mysql.execute(query, function(result) {       
console.log("I am done");

In the above code, database request is made async with the help of callback. So whenever the database query results are available, an event is generated that is handled by the callback & “callback” string is printed out, but before that the event loop moves ahead and prints “I am done”. It’s crucial to note that mysql.execute function itself should be written in an async manner for this to be successful.

Almost all the node api’s are async in nature and can be taken advantage of. But how do I develop my async function? Hmm.. you must read my next post 🙂


This is an introduction post to node.js..

Node.js is an event driven, non blocking (async) I/O style software that is used to develop server side implementations. (If you’re a Python fan, it’s like programming in Twisted!). It’s also built on Google’s V8 JavaScript engine. Like other event driven servers, Node.js, runs an event loop and handles the events asynchronously with callback invocations.

A typical ‘ hello world ‘ web server implementation can be found below. Run as: node helloworld.js

In this example,

1. we import module http and create a http server that listens on port 8888.

2. When the user makes a Get request on http://localhost:8888, the web server renders Hello World on the web browser. You can play around with request and response variables as used in the server code.

3. It’s interesting to note that if reponse.end() statement is commented out, the request doesn’t complete and the server hangs. If you press Ctrl+C, only then the request completes and ‘Hello World’ is rendered on the browser. So server developers, beware!

You may ask, what’s this function(request, response) and why it’s anonymous? Well, that’s a motivation for you to read my next post! 🙂