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

Post to Twitter

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/joyent/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!

Post to Twitter

This entry was posted in JavaScript, Node.js, Open Source, Socket Server, Ubuntu. Bookmark the permalink.

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

  1. Pingback: Trying out WebSocket with Node.js and Google Chrome in Ubuntu 9.10 « Giant Flying Saucer

  2. Pingback: Building a socket server with Node.js and Flash CS4 « Giant Flying Saucer

  3. Pingback: Messing around with Node.js and HTTP « Giant Flying Saucer

  4. Pingback: Getting started using Node.js and Express.js to build a simple website « Giant Flying Saucer

  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

  8. Pingback: Building a socket server with Node.js and Flash CS4 | Giant Flying Saucer

  9. Pingback: Installing Node.js and NPM on Ubuntu 10.04 and try a simple chat application « Giant Flying Saucer

  10. Peter says:

    I followed these steps and it worked on 9.04 – except I had to do a couple of extra steps.

    when I tried to ./configure, it said that there were libssl packages missing. I then tried to make and it gave an error.

    I installed libssl-dev and lib0.9.8 in the synaptic package manager and it worked fine.

  11. Also, in my case, I had to apt-get install pkg-config to get a green light on the ./configure’s answer for openssl

  12. kamil says:

    for openssl you need add:
    sudo apt-get install libssl-dev

  13. molhokwai says:

    Worked just like a charm… Thanks.

  14. Pingback: Installing Node.js and NPM on Ubuntu 10.04 and try a simple chat application | Giant Flying Saucer

  15. Sridhar says:

    Hi,

    First up, thanks for the nice article.

    Small update though:
    git clone git://github.com/ry/node.git
    That url didn’t work . I used git clone git://github.com/joyent/node.git and it worked as expected.

  16. Chad Lung says:

    @Sridhar,

    Thanks, yes they recently moved where NodeJS was hosted on GitHub.

    I’ve updated the article now.

    Chad

  17. Ben says:

    just wanted to say what a great post. I followed all the steps on OSX 10.6 and the only problem I ran into was that it doesn’t work if MAMP (I think LAMP on Linux) is running at the same time. It will tell you that the server is running in Terminal, but won’t work in the browser. I turned MAMP off and it was fine.

    Thanks again

  18. Pingback: Install and try Node.js | Everything stepwise

  19. abercrombie says:

    If you show up as a result of the documentation you are maybe to also see that Node.js has assist obtaining a TCP server, which signifies if you arrive going to really are a Silverlight or Flash/Flex builder you are gonna be while in the place to use Node.js to produce a socket server.

  20. Jonathan says:

    Thanks for the recipe – worked for me on rackspace cloud

    The timeout in the sayhello.js script introduces a delay which might disappoint people who are eager to see a nice quick response. This worked for me too:

    var sys = require(‘sys’),
    http = require(‘http’);
    http.createServer(function (req, res) {
    res.writeHead(200, {‘Content-Type': ‘text/html’});
    res.write(‘    Hello World!‘);
    res.end();
    }).listen(8000);
    sys.puts(‘Server running at http://127.0.0.1:8000/‘);

Comments are closed.