Node.js: First Steps

Yesterday, i have been So yesterday I talked about my experience with node.js in which I was impressed with it's usefulness and went through its history and what node.js brings to the table. A node.js expert, Mr. Rafik NACCACHE highlighted a  couple of features that I did not  mention
like how node.js is best used in apps with WebSocket, like real time networking as seen in games, chat, push notification servers, etc... It should be noted though that node.js isn't the optimal option in certain situations like "classical server rendered web".

Enough talking and let's start the action! in this article I will explain how to install node.js along with its package manager on an ubuntu machine and have some test coding.

Installing

There's an easy way to do it, using the ubuntu package manager of course. We have to add the appropriate PPA to be able to locate the package

sudo apt-get install python-software-properties
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs

If something went wrong or you are too geeky to do it that way, then you probably want to install it from source, and here is how:

echo 'export PATH=$HOME/local/bin:$PATH' >> ~/.bashrc
. ~/.bashrc
mkdir ~/local
mkdir ~/node-latest-install
cd ~/node-latest-install
curl http://nodejs.org/dist/node-latest.tar.gz | tar xz --strip-components=1
./configure --prefix=~/local
make install # ok, fine, this step probably takes more than 30 seconds...
curl https://npmjs.org/install.sh | sh

this is the best way so far to do it, written by Isaac Z. Schlueter. More details can be found here.

Your first script

As i mentioned before, node.js' way to do thing is really super simple and easy, eliminating all the common repetitive coding required to accomplish tasks. Let's make a Hello World web server. First thing we need is a way to initiate a server. Don't worry, that is what modules are for, minimizing the amount of code you have to write. We can use the "http" module which can provide us with a server interface.

var http = require('http');

Now the variable http holds our http server module. What we are going to do next is as follows

  • create a server
  • set its behaviour to give 200 head and echos "Hello World"
  • make it listen on a port

and here is how

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

Pretty easy, right? now let's run it. Save the above code in a file wherever on your hard disk, open terminal, navigate to its folder and type the following:

node script_file_name.js

Open your web browser, type http://127.0.0.1:8100 and hurray! it says Hello World!

Conclusion

Again, node.js is really straightforward and can do big things with just a few simple lines of code. Probably you're saying "hey what about the package manager? we didn't use it", well that is because we didn't do anything complicated yet. In my next article about node.js, i will show you how to install and use more modules and make a slightly more sophisticated program. Until then, follow me on this blog or on social medias and help us by submitting your comments below.

Published: July 15 2013

blog comments powered by Disqus