Node.js Manage app with process manager


It's a good practice to run NodeJS apps controlled by process managers. Process manager helps to keep application alive forever, restart on failure, reload without downtime and simplifies administrating. Most powerful of them (like PM2) have a built-in load balancer. PM2 also enables you to manage application logging, monitoring, and clustering.

PM2 process manager

Installing PM2:

npm install pm2 -g

Process can be started in cluster mode involving integrated load balancer to spread load between processes:

pm2 start app.js -i 0 --name "api" (-i is to specify number of processes to spawn. If it is 0, then process number will be based on CPU cores count)

While having multiple users in production, its must to have a single point for PM2. Therefore pm2 command must be prefixed with a location (for PM2 config) else it will spawn a new pm2 process for every user with config in respective home directory. And it will be inconsistent.

Usage: PM2_HOME=/etc/.pm2 pm2 start app.js