Node.js is an open source, lightweight and efficient cross-platform run time environment for server-side and networking applications. Its written by JavaScript. We can easily build and fast scalable network applications.
Yum Install:
Execute curl command, # curl -sL https://rpm.nodesource.com/setup | bash -
+ rpm -qa 'node|npm' | grep -v nodesource
## Run `yum install -y nodejs` (as root) to install Node.js and npm.
## You may also need development tools to build native addons:
## `yum install -y gcc-c++ make`
Install development tool to build node.js
# yum install -y gcc-c++ make
Now Install node.js,
# yum install -y nodejs
Once installation completed find the location,
# whereis node
node: /usr/bin/node /usr/share/node /usr/share/man/man1/node.1.gz
create a sample file for execute node.js, file name is test.js
My system IP address is 192.168.1.5 and will assign port number to execute node.js : 9001
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello thelinuxfaq.com\n');
}).listen(9001, '192.168.1.5');
console.log('Server running at http://192.168.1.5:9001/');
Run node.js in backend process,
# node /var/www/html/test.js &
Check node.js nunning port number,
# netstat -anp | grep node
tcp 0 0 192.168.1.5:9001 0.0.0.0:* LISTEN 7997/node
# ps aux | grep node
root 7997 0.0 0.6 658820 12732 pts/1 Sl 15:44 0:00 /usr/bin/node /var/www/html/get.js
Finally check your browser with below url,
http://192.168.1.5:9001/
Output:
Hello thelinuxfaq.com
See more details:
# man node
How to Install Manually:
Download the tar.gz file from the URL : http://nodejs.org/download/ using wget command
# cd /usr/local/
# wget http://nodejs.org/dist/v0.10.34/node-v0.10.34.tar.gz
# wget http://nodejs.org/dist/v0.10.34/node-v0.10.34.tar.gz
Extract that the node.js file
# tar -xf node-v0.10.34.tar.gz
Execute file:
/usr/local/node-v0.10.34/bin/node /var/www/html/get.js
Error :
If you may get an error like below,
[root@local]# node /var/www/html/test.js &
Server running at http://192.168.0.13:9001/
events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EADDRNOTAVAIL
at errnoException (net.js:904:11)
at Server._listen2 (net.js:1023:19)
at listen (net.js:1064:10)
at net.js:1146:9
at dns.js:72:18
at process._tickCallback (node.js:419:13)
at Function.Module.runMain (module.js:499:11)
at startup (node.js:119:16)
at node.js:906:3
Solution:
The node cannot listen to your system IP so need to check IP address,
listen(9001, '192.168.1.5');
console.log('Server running at http://192.168.1.5:9001/');
Comments (0)