The amazing adventures of Doug Hughes

Recently, Ive been dipping my toes into languages and technologies I might not previously have. For the last ten years or so ColdFusion has been my bread and butter. Yes, Ive tinkered with other languages like Groovy, but now Im trying to make a more concerted effort to learn more about things I otherwise might not.

Recently Ive been teaching myself a little Ruby and Rails. A local buddy of mine, Bucky Schwarz brought the The Raleigh-area Ruby Brigade to my attention. Last night I attended one of their meetings anticipating learning more about Ruby. However, the topic was not on Ruby, but on NodeJS. The talk was given by Aaron Heckmann.

I recently heard about some developments around JavaScript based servers. Some people are making use of them because they can apparently handle insane loads. Ive heard of some applications handling thousands of connections a second. Last week I tried to Google around and see what I could find, with no luck. So, imagine my surprise when the Ruby meetup topic was about NodeJS, a JavaScript server.

Node JSNodeJS is the brainchild of Ryan Dahl and makes use of Googles open source V8 JavaScript engine. Because of this, NodeJS benefits from the recent competition between Firefox, Google, IE, and Opera on JavaScript engine performance. In other words, NodeJS is very fast.

Installing NodeJS was very simple for me, since Im on OS X. I cant say it will be quite that simple for those in the Windows world since you have to download and build the source code. This was as simple as running ./configure, make, make install. Once NodeJS was installed I was able to run a couple of simple sample applications without any problems.

Much to my surprise, I was able to make small changes to these sample applications because I know JavaScript! I was able to take the HTTP hello world application and change it slightly. The same was true for the network socket sample. Cool!

My initial impression with both of these samples were two-fold:

  1. Wow, that starts up fast! For the simple hello world-style applications there was literally no wait to start NodeJS.
  2. The basic sytax was simple and easy to read. All you need to do to start listening for HTTP requests is this:
var http = require('http');
http.createServer(function (req, res) {}

Now, there are some challenges with Node. In particular, its intended to be pretty low-level. Those of us more comfortable working with higher-level languages might find Node, perhaps, a bit cumbersome.

Thankfully there are a lot of handy modules available for Node. One of these is Express. Express is a small server-side JavaScript web development framework for NodeJS.

To install Express I had to first install Kiwi, which is yet another package management system. To install Kiwi, I simply cloned the Git repository locally using:

git clone http://github.com/visionmedia/kiwi.git

Next, I ran make install in the newly downloaded kiwi directory. For me, this just worked.

Once I had Kiwi I was able to install express by simply running:

kiwi -v install express

Now that I have everything I needed I was able to create copy and paste a quick Express hello world application:

var sys = require("sys"),
kiwi = require("kiwi"),
express = kiwi.require('express')

get('/', function(){
     this.redirect('/hello/world')
})

get('/hello/world', function(){
     return 'Hello World'
})

get('/goodbye/world', function(){
     return 'Goodbye World'
})

run()

In the example above, once you start node running this application, you can hit this application at http://localhost:3000. The default request will redirect you to /hello/world. You can also go to /goodbye/world.

The Express module has a range of other features for routing requests, rendering views, and more. Frankly, I havent taken it much beyond whats in this article yet.

Despite the relative youth of the NodeJS project, it seems to me like theres a lot of energy and excitement behind it. You only need to look at the Module list to realize there are a lot of people working very hard on this project. Just to highlight a few, there are modules to connect to various databases, including MySQL and MongoDB, and many others. There are also modules for logging, templating, testing, and much, much more.

Overall, NodeJS really seems worth trying out and experimenting with.

Comments on: "An Introduction To NodeJS, A JavaScript-Based Server" (1)

  1. Glad I inspired you to check it out! Have fun!

    Like

Comments are closed.

Tag Cloud

%d bloggers like this: