Install node.js using default repository of Ubuntu
Ubuntu 16.04 contains a version of node.js in its default repository.
The version of this node.js is 4.2.6
sudo apt-get update
sudo apt-get install nodejs
To install node.js package manager npm
sudo apt-get install npm
To check node.js version. Type this command -
node -v
npm -v
Install node.js using PPA
Step 1: Adding PPA
$ curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
Step 2: Install Node.js
$ sudo apt-get install nodejs
Now you have done. :)
Lets try an example of node.js
Create a file http_server.js
$ vim http_server.js
add the following content
var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World'); }).listen(3001, "127.0.0.1"); console.log('Server running at http://127.0.0.1:3001/');
Now to run the code on browser
node http_server.js
In browser type in url http://127.0.0.1:3001/
Comments
Post a Comment