PM2

pm2

pm2arrow-up-right makes it possible to utilize all CPUs available on the server. Lets install pm2 on a server.

npm install -g pm2

We are going to create a sample project that will use pm2.

First create package.json with required dependencies and scripts that are going to help us to run the code.

{
  "name": "pm2-demo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "koa": "^2.5.0",
    "koa-router": "^7.4.0"
  }
}

Now we create index.js that will create koa HTTP server and user koa-router to route HTTP requests to our code.

We are done with coding and we want to install dependencies and start the server. npm start will start single instance of NodeJS that will handle our requests.

Lets use all CPUs available and create multiple NodeJS instance on the server.

We can stop all the instances, still using pm2.

Other useful commands are:

Make the server running when server starts up

Last updated