Search This Blog

Monday, May 26, 2014

Installing Node.js on Ubuntu

In this post, we are going to see how to install Node.js (a server side Javscript engine) on Ubutntu. Installing Node.js on Ubuntu is straightforward.

Open a terminal window and enter the following command.

$ sudo apt-get install nodejs



The system will show a prompt to download and install the requisite binaries as follows.


After the installation is complete, open an editor and enter the code to create a small test programme. Our test programme will create an http server.

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



Save the program (I saved mine as test_http.js), and open a terminal window and navigate to the place you have saved your test program. Now enter the following command.

$ nodejs test_http.js



That's it. Our test http server is running.

Now lets open a browser and enter the URL for the server.




There you have it folks. Our first test program with server side javascript with Node.js on Ubuntu.



No comments: