Wednesday, May 4, 2011

Node.js and NPM: Key to Dynamic Web Applications Beyond AMP

I keep reading mention of blog articles on Node.js, Node.js Package Manager (NPM), and web socket programming like this on Twitter and given that I had time to check it out, researched and updated myself on what the rave is all about.

The Node.js website (<rant>) isn't very helpful how to install this supposed to be Javascript file, given that the example invokes the script in a different manner, and you'll even be more confused if we add NPM to the mix. Another disappointment I have against Node.js is that it didn't build on OpenBSD 4.9(</rant>). What I have below is how I was able to set this up on my two Ubuntu 10.04LTS boxes, one with SSL and the other without.

We have to first determine what transport mechanisms are available on your machine. Provided that PHP is installed and properly configured with your development web server, the PHP script below is a very simple and fast way to determine that. Store this on your web server (i.e. with filename getSocketTransport.php) to be accessed by your browser (for other dynamic web scripting languages, this is part of your homework):

$xportlist = stream_get_transports();

This script should display an array list of socket transport protocols. The significance of finding SSL, SSLv2 and/or SSLv3 in that list will be explained later in this article. If you need to understand how to setup SSL on your web server, there are many resources online, google is your friend.

Our next objective is to install Node.js into our web server. Disappointingly, (<rant>) the Node.js website doesn't explain well how this could be done, it took me guidance from this blog and this how to do it and some trial and error (</rant>).

If you do have SSL configured and installed as socket transport as displayed and indicated by the PHP script, you can execute the following from the command line (but this won't work on servers without SSL and sudo may not be needed in your configuration):

git clone
cd node
sudo ./configure
sudo make
sudo make install

Testing to build this on OpenBSD 4.9, it turns out Python is also required. If it complains of not running "waf configure" and it won't make sense because we won't find any executable of that name, we have to have GNU C++ compiler installed on the system, then run the commands again. If from running the previously mentioned PHP script, you don't have SSL, you have to do it like this (note the --without-ssl flag):

git clone
cd node
sudo ./configure --without-ssl
sudo make
sudo make install

If you have successfully followed the steps we provided, from here on you can run "node" from the command line. To verify that node is working, type:

node --version

As of this writing, it should display (which seems more up-to-date than what's on Node.js, v0.4.7):


Now I think we can start playing with this by creating a client/server app and thanks to Jettro Coenradie's blog for this sample code and steps (but my Ubuntu machines have for web server with SSL and for that without SSL). In the example below, I specified the web server with SSL's IP address:

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

You can save this as (in fact, the file extension whether it be .sh or .js isn't an issue, you can even have it .asp and it won't complain) and initiate it with this command (please make sure you're in the same path the script is saved in):


This will create a web server that only replies with the world changingly awesome "Hello World" message as reply when we access the given URL in our web browser. I was concerned that it won't work on my web server without SSL, but it worked as well. I removed the Apache web server from my Ubuntu box, ran the script again and it dawned on me the possibility of dynamic web applications beyond AMP (Apache, PHP/Python/Perl, and MySQL).

Next we install NPM (a.k.a Node.js Package Manager). Based on the resources online, to install NPM you usually type (but this fails in my setup):

sudo curl | sh

I tried the steps below instead and it worked for me on both web servers (with and without SSL):

git clone
cd npm
sudo make install

If your web server doesn't have SSL configured and because of the --without-ssl flag previously, when you run npm later, you will encounter this warning:

WARN shasum crypto binding not found. Cannot verify shasum

On my Ubuntu box with SSL configured, it all went well, on my other Ubuntu box without SSL, within the npm folder I had to run this command:

sudo node ./cli.js install -g

From here on, you should be able to run npm now by typing to get it's quick help options:

npm -h

On both my web server, no module was automatically installed, is a popular pmodule and to install it, we have to type:

sudo npm install

To verify installed modules and you should see listed we have to type as below and it should display as one of the entries on the list:

npm list

You can find more packages/modules you can install from this list.

What does this all mean to web application development? With Node.js, NPM and the wide array of NPM packages, it's now possible to setup a web application stack that is written in pure Javascript, has numerous MVC, ORM (like for PostgreSQL and SQLite) and workflow frameworks. Add to that possibility of developing a real-time server push web application using that communicates in every popular socket transport protocol, it beats every other popular web application programming language feature after feature. The hype I read online and Twitter is very much deserved.

No comments:

Post a Comment