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:

Installation:

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)
Advertisements

Node.js

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! 🙂

Python Tornado Webserver

Tornado is a open-source, fast, scalable non-blocking web server used for development of real time web services. It solves the c10k problem by using epoll library and can handle thousands of simultaneous connections. It runs as a single threaded non-blocking io loop that waits on requests (events) from users and servers them (hence also known as event-triggered web server). Unlike blocking web servers where thread management, context switching becomes expensive, Tornado gains in thread safety.

See below for prerequisites, installation steps along with an example of Hello World program in Tornado. Don’t worry about many things right now, just see how Tornado web server runs and realize the ease of web development with Tornado! 🙂

This post would be followed by other posts detailing about the capabilities of Tornado web framework. Please note: We use, Ubuntu 10.04, Python v2.6 and tornado2.2 for all the posts.

Prerequisites

Python 2.5+
PycURL for python v2.6
For other python version you could visit tornado

Installation

tar xvzf tornado-2.x.tar.gz 
cd tornado-2.x 
python setup.py build 
sudo python setup.py install