Getting started with nodejs : 7 steps to your first HTTP server on nodejs

[Infragistics] Murtaza Abdeali / Monday, March 12, 2012

There is ton of innovations going on these days around client frameworks and stacks using JavaScript and HTML5, among them is a unique server framework out that you can code against using JavaScript, it is called nodejs. Nodejs is a light weight framework build on top of Chrome’s V8 JavaScript Runtime. One of the key features is it’s non-blocking I/O model and you just code everything in JavaScript of course. Nodejs is based on a modular design, the base framework is pretty light, but the community has contributed a bunch of nodejs packages. You can pick and choose the ones you want to use within your server side framework. This makes it very easy to control your server-side application foot print and build up the server side pieces based on what you need within your applications. There are a total of 8K packages(nodejs modules) within NPM (Nodejs package manager).

So, let’s get started and build our first web server using nodejs, it is quite simple. I am going to show you how to download nodejs on your windows machine, there is a Mac installer available for it as well.

Building HTTP Web Server

1. Go to nodejs website to download the msi. (http://www.nodejs.org)

2. Click on the download button and then choose windows installer MSI.

3. Once you’ve download the MSI, you can run the installer and follow through the instructions. It is a three screen wizard as you can see below. Click “next” on the first two screens and finally “finish.”

You have now installed nodejs on your machine.

4. Go to program files and there should be a folder called “nodejs.” Open that folder and you should see the following files in there.

5. Now, we are going to create a JavaScript file called helloworld.js and write up a simple HTTP server that is going to return text “Hello World” when called from the browser. This JavaScript file is going to sit in the same folder as the rest of the nodejs files and folders. The JavaScript code needed to create the HTTP server is going to run on port 1337 of local host and is going to return “Hello World” when called is as follows:

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

Let’s put this code in a file and save it at the same location as the rest of nodejs files.

6. Now that we have our file, we need to tell node to execute it and it will create an HTTP server that is going to start listening on port 1337. The command to execute node is :

> node <filename>.js

In our case, to run helloworld.js, we’ll run the following command to spin up our HTTP server.

> node helloworld.js

Having done that, our HTTP server will spin up and the createServer function is going to tell the server to listen on port 1337, you’ll notice that the console logged even before we made the request, which proves nodejs’ non-blocking model.

7. Finally, open up the browser and go to port 1337 on your localhost. You see the HTTP server picking up our request and sending “Hello World” response.

Finally, you have your first HTTP server on nodejs taking in your request from port 1337 and return “hello world.”

Thing are looking very promising for nodejs’ future. It’s pluggable model means anyone can write an SDK on top of it and build a power JavaScript based server-side plug-in, there are about 8K packages available today. Packages include a full blown web framework written on top of nodejs called express. There are nodejs database drives for mysql and drizzle. There is also a driver available for NoSql  MongoDB database. Even, windows azure SDK is available as a nodejs package.

At  Infragistics, we are constantly looking for feedback from the community to continue to build tools that can help with your development with JavaScript, jQuery and HTML5 stacks. If you are developing applications around nodejs, I would like to hear from you. I can be reached at : murtazaa@infragisics.com

For latest updates follow me on twitter : @mabdeali