How To Run Node JS Server In Background

When you use Node JS to implement a http / https web server, you always follow the general steps as below. But if you close the terminal or kill the node process unexpectedly, the http web server will stop also. This article will tell you how to avoid such issue and make the node http server continuously running at background even it is stopped.

1. General Steps To Run Node Http Web Server.

  1. Create a js file which will start a http web server on special port.
    http_server.js

    // Include http module.
    var http = require('http');
    
    // Create http server.
    var httpServer = http.createServer(function (req, resp) {
    
        resp.writeHead(200, {'Access-Control-Allow-Origin':'*','Content-Type': 'text/plain'});
        resp.write("Welcome to dev2qa.com.");
        resp.end();
    });
    
    // Start http server listen on port 8888.
    httpServer.listen(8888);
    
    console.log("Use browser to get url 'http://localhost:8888/http_server.js'");
  2. Open a terminal and run below command to start the http web server.
    sh-3.2# node http_server.js 
    Use browser to get url 'http://localhost:8888/http_server.js'
  3. Open a browser and browse url http://localhost:8888/http_server.js, then you can get below web page that means the http web server has been started successfully.
    node js http web server page
  4. Run ps -ef|grep node command in terminal, you can see there are two processes in the list, one is the ps command the other is the node http web server process.
    192:~ zhaosong$ ps -ef|grep node
          4166  3516   0 12:33PM ttys000    0:00.22 node http_server.js
      501  4226  4213   0 12:39PM ttys003    0:00.01 grep node
    

2. Make Node JS Server Run At Background.

In above method, Node JS server will always occupy the terminal until the terminal is closed. But when you close the terminal, Node JS server will also stop. To fix this issue, you can start the Node JS serve with nohup command as below.

sh-3.2# nohup node http_server.js > output.log &
[1] 4327
sh-3.2# ps -ef|grep node
      4327  3516   0 12:52PM ttys000    0:00.19 node http_server.js
      4329  3516   0 12:52PM ttys000    0:00.01 grep node

The nohup command will run a command at background, in this case it will start the Node JS http web server at background and display the node server process id. All the server side output will be saved in output.log file. Excellent, but when the node process is killed unnormal ( $ kill 4327 ) , the http server stop also.

READ :   How To Create Custom NPM Modules

3. Run Node JS Server At Background Continuously With Forever.

Forever is a Node JS package which can make a node script execute forever even after the node script process is killed. It will execute the node js script in a new process when the old process has been stopped suddenly. To use forever, follow below steps.

  1. Install Node forever package. Below command will install the forever package globally.
    sh-3.2# npm install forever -g
  2. After install, run npm list command to see the installation path. Generally npm package is installed in /usr/local/lib/node_modules folder.
    sh-3.2# npm list forever -g
    /usr/local/lib
    └── [email protected]
    
  3. Start node http web server with forever start command. From below output message, you can see first forever start the http web server in process with id 4854, after you kill that process, forever start another process ( id is 4872 ) to run the node http web server immediately. So the http web server will run at background continuously.
    sh-3.2# forever start http_server.js 
    ......
    info:    Forever processing file: http_server.js
    sh-3.2# ps -ef|grep node
          4853     1     2:51PM ??         0:00.36 /usr/local/bin/node /usr/local/lib/node_modules/forever/bin/monitor http_server.js
          4854  4853     2:51PM ??         0:00.10 /usr/local/bin/node /Users/zhaosong/Documents/WorkSpace/dev2qa.com-example-code/JavaScriptExampleWorkspace/NodeJSWorkspace/RunScriptForever/http_server.js
          4857  3516     2:51PM ttys000    0:00.00 grep node
    
    sh-3.2# kill 4854
    sh-3.2# ps -ef|grep node
          4853     1     2:51PM ??         0:00.37 /usr/local/bin/node /usr/local/lib/node_modules/forever/bin/monitor http_server.js
          4872  4853     2:53PM ??         0:00.12 /usr/local/bin/node /Users/zhaosong/Documents/WorkSpace/dev2qa.com-example-code/JavaScriptExampleWorkspace/NodeJSWorkspace/RunScriptForever/http_server.js
          4874  3516     2:53PM ttys000    0:00.00 grep node
  4. You can use # forever list command to list all forever running processes.
  5. # forever stop command can stop the running node script.
    sh-3.2# forever stop http_server.js 
  6. For more information about forever, you can refer to it’s official website
(Visited 21 times, 1 visits today)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.