Getting started with Node.js on Ubuntu 9.10 in about 5 minutes

I recently installed Ubuntu 9.10 on VMWare 6.5 Workstation and wanted to try out Node.js. According to the Node.js website Node is a way to provide an easy way to build scalable network programs. The nice thing about Node is that it is event driven (making use of Javascript’s strength with events and also making it easy to learn) and there are no “locks”. Node is similar to Ruby EventMachine or Python Twisted. There is already a strong user community here.

Lets go ahead a build a couple simple apps using Node.js. I’ll assume from here that you already have Ubuntu 9.10 already running.

On a clean install of Ubuntu 9.10 I quickly found out that git was not installed by default. You can fix that by going to the terminal and running this command (you’ll probably need to use “sudo” before the commands):

apt-get update
apt-get install git-core
apt-get install build-essential

Once GIT is install you can then clone the Node.js repository:

git clone git://github.com/ry/node.git

Move into the new “node” directory:

cd node

Now configure and install:

./configure
make
sudo make install

This should work flawless (at least it did for me).

At this point you can take the demo from the Node.js homepage and copy or type in the code. I’ve taken the demo code and slightly modified it.

var sys = require('sys'),
   http = require('http');
http.createServer(function (req, res) {
  setTimeout(function () {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write('<br/><strong>&nbsp;&nbsp;&nbsp;&nbsp;Hello World!</strong>');
    res.end();
  }, 2000);
}).listen(8000);
sys.puts('Server running at http://127.0.0.1:8000/');

I named my file “sayhello.js”. Now you can run the code like this:

Fire up your browser to this address and you should see the result.

At this point you can start looking into some examples here. You can also play with the API as well.

In this next example I’m simply dumping out the HTTP Request object to inspect it. The code used for this is in line #8.

var sys = require('sys'),
   http = require('http');
http.createServer(function (req, res) {
  setTimeout(function () {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write('<br/><strong>&nbsp;&nbsp;&nbsp;&nbsp;Hello World!</strong>');
    res.end();
    sys.puts(sys.inspect(req, false));
  }, 2000);
}).listen(8000);
sys.puts('Server running at http://127.0.0.1:8000/');

If you look through the documentation you will also see that Node.js has support to be a TCP server, which means if you are a Silverlight or Flash/Flex developer you can use Node.js to build a socket server.

Enjoy working with Node.js!

7 Responses to “Getting started with Node.js on Ubuntu 9.10 in about 5 minutes”

  1. [...] Giant Flying Saucer Programming Parlor Tricks « Getting started with Node.js on Ubuntu 9.10 in about 5 minutes [...]

  2. [...] you haven't worked through my Node.js tutorial do that first because I'm going to assume you have Node.js up and [...]

  3. [...] done a few articles/tutorials on using Node.js. Today though I'm going to simply build a simple HTTP [...]

  4. [...] you will need to have Node.JS installed. You can check out an earlier article of mine that shows you how to do that under Ubuntu 9.10. I am currently running Node.js under [...]

  5. Pk Anane says:

    Well good examples.. however I had a problem building node on my karmic koala with your method.. I solved it however by first downloading the tar.gz node source from nodejs.org installing the required dependencies (openssl and g++) before i run the examples you provided.. i’m definitely going to investigate further.. l8r

  6. Doug says:

    You should tell people to run apt-get install build-essential

    too, first to get the compilers and so forth installed.

  7. Chad Lung says:

    @Doug,

    Good point. I’ll update the article.

    Chad

Leave a Reply