#### What is Node.js?
Node.js is a brilliant platform for creating network applications. It is mainly known for its non-blocking I/O and event driven system. In simple terms, Node.js can easily handle a large number of requests while simultaneously consuming lesser server memory. These are the attributes make Node.js a better than other languages and platforms.
Why use Node.js?
Node.js comes with a built in HTTP server library. This means it doesn’t require the help of any external piece of software to act as a web server. Using Node.js alone one can have greater control of the web server parameters.
We will be installing Node.js on Ubuntu using Chris Lea PPA. The PPA will keep you will up-to-date to the latest stable version of Node.js depending on your Ubuntu release, The PPA might also download required dependencies depending on you Ubuntu install type.
There is a naming conflict with the node package (Amateur Packet Radio Node Program), and the nodejs binary has been renamed from node to nodejs. You’ll need to symlink /usr/bin/node
to /usr/bin/nodejs
or you could uninstall the Amateur Packet Radio Node Program to avoid that conflict.
Lets get it up and running!
- You need to update you local repository database by entering the command below.sudo apt-get update
- Adding the PPA.sudo add-apt-repository ppa:chris-lea/node.js
- Re-updating the local repository database to add the PPA to the local repository database.sudo apt-get update
- Installing Node.js using the PPA
This will also install NPM.
sudo apt-get install nodejs
“`
#### Writing you first Node.js App.
- We need to create a file named
app.js
.app
is the name.js
stands for javascript file.js
is always used to specify that the file is a javascript file. - Add the lines given below.
var http = require("http"); server = http.createServer(function (request, response) { console.log("New request. Request url:" + request.url); response.writeHead(200, {"Content-Type":"text/plain"}); response.end("Hello!"); }); server.listen(3000); console.log("Starting Node.js server"); console.log("Running at 127.0.0.1:3000");
Code language: JavaScript (javascript)
You can visit http://127.0.0.1:3000
to see the app in action.
Now we have Node.js installed on Ubuntu. If you have any problem feel free to leave a comment below.