paint-brush
Setup a Node.js Application Using PM2by@programming
5,204 reads
5,204 reads

Setup a Node.js Application Using PM2

by kiranOctober 27th, 2019
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

PM2 is a free open source, advanced, efficient and cross-platform production-level process manager for Node.js with a built-in load balancer. It works on Linux, MacOS as well as Windows. It supports app monitoring, efficient management of micro-services/processes, running apps in cluster mode, graceful start and shutdown of apps. It keeps your apps “alive forever” with auto restarts and can be enabled to start at system boot.

Company Mentioned

Mention Thumbnail
featured image - Setup a Node.js Application Using PM2
kiran HackerNoon profile picture

What is PM2

PM2 is a free open source, advanced, efficient and cross-platform production-level process manager for Node.js with a built-in load balancer. It works on Linux, MacOS as well as Windows. It supports app monitoring, efficient management of micro-services/processes, running apps in cluster mode, graceful start and shutdown of apps.

It keeps your apps “alive forever” with auto restarts and can be enabled to start at system boot, thus allowing for High Availability (HA) configurations or architectures

Why to Use PM2

Assume you have a micro service in node js (

server.js
) running on port 7890.

const http = require('http');

const hostname = '192.168.43.31';
const port = 7890;

const server = http.createServer((req, res) => {
	res.statusCode = 200;
  	res.setHeader('Content-Type', 'text/plain');
  	res.end('This is the Main App!\n');
});

server.listen(port, hostname, () => {
  	console.log(`Server running at http://${hostname}:${port}/`);
});

The above code will run on server after executing node server.js command. But what if something crashes in this API. We need to restart it manually by executing the same command line.

Here PM2 comes to rescue by restarting the node api automatically. You will not run your app as root; therefore, your app will be more secure.
Your application will restart if it crashes, and it will keep a log of unhand led exceptions.

Your application will restart when the server starts — i.e. it will run as a service.

How to Do It

Use npm to install process manager for Node.js using following command. This will install latest version of pm2 on your system.

npm install pm2@latest -g

Now start your node API by simply executing the following command.

pm2 start server.js
# Specify an app name
pm2 start server.js --name <app_name>

Managing The Process

Managing application state is simple here are the commands:

$ pm2 restart app_name
$ pm2 reload app_name
$ pm2 stop app_name
$ pm2 delete app_name

List the status of all application managed by PM2:

$ pm2 [list|ls|status]

To display logs in realtime.

$ pm2 logs

PM2 Cluster Mode

The cluster mode allows networked Node.js applications (http(s)/tcp/udp server) to be scaled accross all CPUs available, without any code modifications. This greatly increases the performance and reliability of your applications, depending on the number of CPUs available.

Under the hood, this uses the Node.js cluster module such that the scaled application’s child processes can automatically share server ports.

To enable the cluster mode, just pass the

-i
option:

pm2 start server.js -i max

max
 means that PM2 will auto detect the number of available CPUs and run as many processes as possible

So i think you got a basic idea about pm2 and how to use it.

Please refer the below url for further informations.
https://pm2.keymetrics.io/