Node.js - Getting started on Ubuntu 14.04 (Trusty Tahr)

In this article we are going to have look into the installation and basic usage of node.js application. Node is a set of libraries for JavaScript which allows it to be used outside of the browser. It is primarily focused on creating simple, easy to build network clients and servers.


You will have to download the package from the official site here. After the download type the following commands in the terminal.

	tar -xzf node-v0.x.x.tar.gz
	cd node-v0.x.x.tar.gz
	sudo make install

The above commands should do the installations now lets go for the dependencies required.

apt-get -y install build-essential

Now after the installation is completed just check it by typing:

The above commands are just to verify that your nodejs is installed properly. For futher information about the iinstallation click here.

First Program.

Its time that we start with the basic famous program "Hello World!." Just copy this code into any text editor and save it as "your-name.js".

console.log('Hello World!.');

Now save that file and type in the following command:

This should print the infamous output on the terminal.

hello world on http server.

Now after the above, we are going to work with it on a http server or your localhost. Copy and paste following code in to your editor:

	var http = require('http');
	var server = http.createServer(function(req, res) {
		res.end('Hello Http');

And save it as "http.js". Now goto the terminal and type in:

node http.js

This will look something like

The first thing you'll notice is that this program, unlike our first one, doesn't exit right away. That's because a node program will always run until it's certain that no further events are possible. In this case the open http server is the source of events that will keep things going. Testing the server is as simple as opening a new browser tab, and navigating to the following url: http://localhost:8080/. As expected, you should see a response that reads: 'Hello Http'.

You can also check that by opening a new terminal window and typing:

Now let's have a closer look at the steps involved in our little program. In the first line, we include the http core module and assign it to a variable called http. You will find more information on this in the next section about the module system. Next we create a variable called server by calling http.createServer. The argument passed into this call is a closure that is called whenever an http request comes in. Finally we call server.listen(8080) to tell node.js the port on which we want our server to run. \ If you want to run on port 80, your program needs to be executed as root. Now when you point your browser to 'localhost:8080', the connection closure is invoked with a req and res object. The req is a readable stream that emits 'data' events for each incoming piece of data (like a form submission or file upload). The res object is a writable stream that is used to send data back to the client. In our case we are simply sending a 200 OK header, as well as the body 'Hello Http'. So here are the basics of how to use node.js anny quuestions comment below.


Share this page:

4 Comment(s)