“Node.js® is a JavaScript runtime built on Chrome’s V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js’ package ecosystem, npm, is the largest ecosystem of open source libraries in the world.” — Defined on nodejs.org

We will use PPA to Ubuntu 16.04 LTS (Trusty Tahr) and 14.04 LTS (Xenial Xerus) Systems and install node.js in few steps. Node.js is available in two versions, first is current  8.2.1 and another is LTS 6. 11.1.

To install current 8.2.1 use following command in your terminal

$ sudo apt-get install python-software-properties
$ curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -

To install LTS 6.11.1

$ sudo apt-get install python-software-properties
$ curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -

Now we have required PPA file lets install Node package. NPM will also be installed with node.js. Use below command to install Node.js
$ sudo apt-get install nodejs

Please use the following command to check the version of the NPM and Node.

$ node -v

for NPM

$ npm -v

Now lets create a demo program using node.js and test it in browser. Here you will learn how to run a node program in browser.

$ gedit http_server.js

It will open the editor. Put the following lines of code in it and save it.

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

now start your node server and test the program.

$ node http_server.js

debugger listening on port 5858
Server running at

As you can see our program is listing port 3000. The web server has been started on port 3000. Now you can see the output in browser using following url.

Thanks & regards

Share This