nodejs forever as webserver (Run the server forerver)

Posted by

nodejs ‘http/https’ can be used as a stand-alone web server without the need for a separate server like Apache or nginx with disadvantage of it can be used to deploy UI Applications for now.

Lets see how to setup a simple http nodejs server below

Basic HTTP Server (server.js) :

var util = require('util'),  
    http = require('http');

http.createServer(function (req, res) {  
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.write('Hello, DevOps!')
  res.end();
}).listen(8443);

/* server started */  
util.puts('> DevOps!');

Start the server using node:

$ node server.js 
> DevOps!
(node:15418) DeprecationWarning: util.puts is deprecated. Use console.log instead.

Test the server :

$ curl -k http://localhost:8443
Hello, DevOps!

$ forever list
info:    Forever processes running
data:        uid  command                                                       script    forever pid   id logfile                          uptime       
data:    [0] gGr9 /node-v6.9.1-linux-x64/bin/node server.js 16690   16700    /.forever/gGr9.log 0:0:1:41.968

Identify the running process and kill it:

$ ps ax|grep node
18006 pts/1    Sl     0:00 node server.js

kill -9 18006

Now lets see how forever nodejs package can be used to run the server forever.

How to install forever ?

$npm install -g forever

Start http server using forever:

$ forever start server.js 
warn:    --minUptime not set. Defaulting to: 1000ms
warn:    --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
info:    Forever processing file: server.js

$ curl -k http://localhost:8443
Hello, DevOps!

forever list of running servers
$ forever list
info:    Forever processes running
data:        uid  command                                                       script    forever pid   id logfile                          uptime       
data:    [0] gGr9 /node-v6.9.1-linux-x64/bin/node server.js 16690   16700    /.forever/gGr9.log 0:0:1:41.968 

Logs are written to this location : /.forever/gGr9.log

Stop http server using forever:

fetch list of forever process running and pass the index of the process or process Id to stop command

$ forever list
info:    Forever processes running
data:        uid  command                                                       script    forever pid   id logfile                          uptime       
data:    [0] gGr9 /node-v6.9.1-linux-x64/bin/node server.js 16690   16700    /.forever/gGr9.log 0:0:1:41.968 
$ forever stop 0
info:    Forever stopped process:
    uid  command                                                       script    forever pid   id logfile                          uptime       
[0] gGr9 /node-v6.9.1-linux-x64/bin/node server.js 16690   16700    /.forever/gGr9.log 0:0:1:52.198

Test the server after stopping:

$ forever list
info:    No forever processes running
$ curl -k http://localhost:8443
curl: (7) Failed connect to localhost:8443; Connection refused

Redirect logs to specific location?

$forever start server.js > server.log

nodejs https/ssl setup :

Sample https web server(server-ssl.js):

var fs = require('fs');
var https = require('https');
var app = require('express')();
var options = {
   key  : fs.readFileSync('server.key'),
   cert : fs.readFileSync('server.crt'),
   secureOptions : 'tlsv1.2'
};

app.get('/', function (req, res) {
   res.send('Hello DevOps!');
});

https.createServer(options, app).listen(8443, function () {
   console.log('Started!');
});

Install required node dependencies:

$ npm install express

Create Self signed certificate using openssl:

Generate private key
$openssl genrsa -des3 -out server.private.key 1024

Generate CSR 
$openssl req -new -key server.enc.key -out server.csr

Remove password from private key
$openssl rsa -in server.private.key -out server.key

Generate self signed cert
$openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

Start the server:

forever start server-ssl.js

Test the server:

$curl -vk https://localhost:8443

* About to connect() to localhost port 8443 (#0)
*   Trying ::1...
* Connected to localhost (::1) port 8443 (#0)
* Initializing NSS with certpath: sql:/etc/pki/nssdb
* skipping SSL peer certificate verification
* SSL connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
* Server certificate:
* 	subject: E=devopsunixandjava@gmail.com,CN=devopsunixandjava.com,OU=Devops,O=Devops,L=***,ST=***,C=***
* 	start date: Mar 06 09:02:15 2017 GMT
* 	expire date: Mar 06 09:02:15 2018 GMT
* 	common name: devopsunixandjava.com
* 	issuer: E=devopsunixandjava@gmail.com,CN=devopsunixandjava.com,OU=Devops,O=Devops,L=***,ST=***,C=***
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: localhost:8443
> Accept: */*
> 
< HTTP/1.1 200 OK
< X-Powered-By: Express
< Content-Type: text/html; charset=utf-8
< Content-Length: 13
< ETag: W/"d-deYWBzwbwuo2wgQ09hUSK/rew7w"
< Date: Mon, 06 Mar 2017 09:08:34 GMT
< Connection: keep-alive
< 
* Connection #0 to host localhost left intact
Hello DevOps!

configure SSL/TLS protocol:

secureOptions : 'tlsv1.2'

Leave a Reply

Your email address will not be published.