HOWTOlabs  
 Services     Software     Commentary     Design     Astral Musings   
node.js
tips and best practices

node.js
rickatech 2015-05

Related

Elsewhere

Reference [edit]

Updating this page for CentOS 7, systemd, and more modern yum repositories.

RHEL/CentOS 7 Install
rickatech 2015-07

Elsewhere [edit]

Core Node.js functionality has not yet been mainlined into the base RHEL repositories, however the venerable and stable EPEL repository provides a viable workaround.  The node community has its own captive package management system called npm not surprisingly.  Though npm is not mandatory to be able to run service side javascript, odds are there will be something one needs that is best obtained/updated through that service.

# cat /etc/redhat-release 

  CentOS Linux release 7.1.1503 (Core) 

# yum install epel-release

# yum install nodejs

# rpm -qa

  36a37
  > libuv-0.10.34-1.el7.x86_64
  > v8-3.14.5.10-17.el7.x86_64
  > nodejs-0.10.36-3.el7.x86_64
  > gpg-pubkey-352c64e5-52ae6884
  > http-parser-2.0-4.20121128gitcd01361.el7.x86_64
  > epel-release-7-5.noarch
  > c-ares-1.10.0-3.el7.x86_64

# node --version

  v0.10.36

# yum install npm

# rpm -qa

  38a39
  > nodejs-semver-2.1.0-3.el7.noarch
  > nodejs-rimraf-2.2.2-1.el7.noarch
  > ...
  > gcc-c++-4.8.3-9.el7.x86_64
  > npm-1.3.6-5.el7.noarch 

$ cat example.js 

  var http = require('http');

  http.createServer(function (req, res) {
	res.writeHead(200, {
		'Content-Type': 'text/plain'
		});
	res.end('Hello World\n');
	}).listen(1337, "127.0.0.1");

  console.log('Server running at http://127.0.0.1:1337/');

$ node example.js

  [ switch to another local terminal ]

$ wget http://localhost:1337/any

$ cat any 

  Hello World 

systemd
rickatech 2015-08

Elsewhere

Although Node.js provides a basic framework for running server side javascript in a manner that can accept web/http requests, it does not provide a standard daemon to allow this to occur in the background like venerable web services like Apache/httpd.  Fortunately this is something Linux/Unix is extraordinarily gifted at supporting, and with more recent systemd services, it is rather straightforward to place some directives in a daemon file to allow as many javascripts applets to run asynchronous as you like.

# pwd

  /etc/systemd/system

# ls -lh no*

  nodesvr-ricktest.service

$ systemctl status nodesvr-ricktest

  nodesvr-ricktest.service - Node.js Example Server
  Loaded: loaded (/home/rickatech/node/sysd)
  ...

$ cat nodesvr-ricktest.service

  [Unit]
  Description=Node.js Example Server
  #Requires=After=mysql.service       # Requires mysql service to run first

  [Service]
  # ExecStart=/usr/local/bin/node /opt/nodeserver/server.js
  ExecStart=/bin/node /home/rickatech/node/example.js
  Restart=always
  RestartSec=10               # Restart service after 10 seconds if crashed
  StandardOutput=syslog       # Output to syslog
  StandardError=syslog        # Output to syslog
              
  # SyslogIdentifier=nodejs-example
  SyslogIdentifier=nodejs-ricktest
  #User=
  User=rickatech
  #Group=
  Environment=NODE_ENV=production PORT=1337

  [Install]
  WantedBy=multi-user.target 

$ cat example.js 

  var http = require('http');

  http.createServer(function (req, res) {
	res.writeHead(200, {
		'Content-Type': 'text/plain'
		});
	res.end('Hello World\n');
	}).listen(1337, "127.0.0.1");

  console.log('Server running at http://127.0.0.1:1337/');  

proxy
rickatech 2015-08

Elsewhere

For small hosting environments without load balancers and such it can be handy to be able to run straight Apache with PHP and Node.js on the same server.  Using ProxyPass allows Apache to pass a particular URL path to a local service.  This means Node.js services can be run independently of Apache locally on different ports while Apache can unify them into a URL based directory that serves static web and dynamic PHP requests for directories not mapped to Node.js .  One thing to be mindful of is server sessions state - stay tuned for more on that.

<Directory /public/np1/site>
#   AllowOverride All
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
/Directory>

VirtualHost *:80>
    ServerName np1.local.zaptech.org
    DocumentRoot /public/np1/site
#   ErrorDocument 503 /maintenance.html

    ProxyRequests on
#   ProxyPass /maintenance.html !
    ProxyPass /np1/ http://localhost:1337/
</VirtualHost>  
Redis

Miscellany

ARCHIVE
nodes.js
circa 2011−12

[ reveal section ]  
zap technologies
tablet | printable