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.
Insatallation.
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
./configure
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.writeHead(200);
res.end('Hello Http');
});
server.listen(8080);
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: