Installing node.js on centos 6 with sample app

Node.js is a platform that works on V8 JavaScript Engine .it uses an event-driven function which is very useful for hosting real-time applications.
In this article , we can see about installing node.js on centos 6 and running a sample application .

Installing node.js on centos 6

Let’s start, Installing node.js on centos 6 is very simple.
Step 1 » You need to enable EPEL repository which contains node.js packages and dependencies . Find the latest release file from EPEL 6 repository and install using rpm command.
[root@krizna ~]# rpm -ivh http://epel.mirror.net.in/epel/6/i386/epel-release-6-8.noarch.rpm
Step 2 » Now start installing node.js and dependencies.
[root@krizna ~]# yum install npm --enablerepo=epel
Step 3 » That’s it, installation is over. Now check your installation with a simple “hello world” example in the command prompt.
[root@krizna ~]# node
> console.log("Hello world")
Hello world
undefined

Great!! your installation is perfect. Press “CTRL+C” twice to exit..

Running sample app

Step 4 » Let’s try a “hello world” web application . Create a directory (optional 🙂 ) and a file .
[root@krizna ~]# mkdir sample
[root@krizna ~]# cd sample
[root@krizna sample]# vim test.js

Add the below code to that file.( About the code )

http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello Worldn');
}).listen(2000);
console.log('Server running at port 2000');

Step 5 » Now run the code using node command.
[root@krizna sample]# node test.js
Server running at port 2000

Step 6 » Now your test application is running on port 2000 . open your browser and goto http://IPaddress:2000 . you will get the page like below. Installing node.js on centos 6
if you are not getting the page , Disable Iptables service on your server .
Disable firewall ( Iptables ) »
[root@krizna ~]# service iptables stop
[root@krizna ~]# chkconfig iptables off

Step 7 » Now We can check some complete example site. this site is designed using expressjs ..( What is express.js ?? ) .
Install GIT and clone the repository.
[root@krizna sample]# yum install git
[root@krizna sample]# git clone https://github.com/shapeshed/express_example.git

Step 8 » Now install site modules using npm command.
[root@krizna sample]# cd express_example/
[root@krizna express_example]# npm install

Step 9 » launching the site.
[root@krizna express_example]# node app.js
Express server listening on port 3000

Step 10 » Open http://IPaddress:3000 on your browser.
Installing node.js on centos 6
Additionally install forever tool to run the code in the background .
Forever Installation and Usage.
[root@krizna express_example]# npm install forever -g
Starting test app created earlier ( Step 4 ).
[root@krizna sample]# forever start test.js
Starting sample express example app.
[root@krizna express_example]# forever start app.js
Listing
[root@krizna express_example]# forever list
info: Forever processes running
data: uid command script forever pid logfile uptime
data: [0] NcMN /usr/bin/node test.js 2110 2112 /root/.forever/NcMN.log 0:0:1:29.91
data: [1] xYL8 /usr/bin/node app.js 2134 2136 /root/.forever/xYL8.log 0:0:0:4.775

Restarting and stopping using their ID.
[root@krizna express_example]# forever restart 1
[root@krizna express_example]# forever restartall
[root@krizna express_example]# forever stop 1
[root@krizna express_example]# forever stopall

Use -w option to restart app automatically after changes .
[root@krizna express_example]# forever start app.js -w
Ok .. That’s all i have got .. Have a great day.

2 Comments

Leave a Reply

Your email address will not be published.


*