Node JS Process Managers | pm2 and forever
We all as a developer, work very hard in creating different API, microservices for different products. Once we are done with all the development on our local machine, now it is time to deploy/host the code somewhere. Out of the available options, you can choose one of the following
Platform as a service(PAAS) Example Google App Engine, Heroku, etc.
Infrastructure as a service(IAAS) Example Digital Ocean, IBM, Amazon web services.
But you should be aware of the steps to deploy your service, otherwise, even if you develop an industry-changing product, hardly anyone would be able to use them. If we talk about PAAS, every platform has altogether different ways to deploy the code. The steps to deploy on any particular platform you will be required to find on the respective platform's documentation. But here we will discuss IAAS since the commands remain the same. The only difference that you might encounter is as soon as you spin up a new server, it could have a different Operating system like different flavors of Linux (Ubuntu, Redhat, Fedora), Windows. We will be considering Linux as our base operating system, but the steps to deploy your code on other operating systems will remain somewhat similar(at least basic concepts/commands will remain the same).
Majorly we will be discussing 2 process managers i.e. Pm2 and forever. There are many other ways like dockerizing your NodeJS app etc., that you can use to spin up your service, but that will be a different topic to discuss.
PM2:
NPM documentation for pm2 can be found here: https://www.npmjs.com/package/pm2
So before starting pm2, by far pm2 is my favorite process manager for the NodeJS application since it gives lots of features out of the box. Pm2 is a famous process manager for Nodejs, with various features like analytics, monitoring, heap map generation, load balancing, etc. Let run a sample code using pm2, that will help you understand it better.
var http = require('http');
//create a server object:
http.createServer(function (req, res) {
res.write('Hello World!'); //write a response to the client
res.end(); //end the response
}).listen(8080); //the server object listens on port 8080
Here if are already working with NodeJS, you must have already encountered this code. So just to explain, we are creating an HTTP server of NodeJS, that is listening on port 8080. Make sure no other process is running on port 8080, otherwise you will get an error that "port already in use". In case you get this error change the port in the code to some other value. This checks the list of all the processes and the port on which all services are running.
netstat -nlett
Now after you save the code in as "index.js" file, open the terminal, and run the "index.js" file using the command.
node index.js
Open any of your popular browsers, and open http://localhost:8080/, you will see "Hello world" printed on your browser screen.
Till now things were basic, just a small recap on how we run NodeJS service and we all know as soon as we close our terminal "NodeJs" service will get killed.
Prerequisite:
You should have npm, NodeJS installed.
If you have npm installed, you can simply install the pm2 module globally on your system.
npm install -g pm2
To double-check if pm2 is installed successfully. Type pm2 on terminal
Now let discuss how we can run the service, using obviously our beloved pm2. There are two ways you can pm2 service. First Method:
Directly running the service using pm2 by accessing the index.js file. Use the following command
pm2 start index.js
After successfully starting the process, you can check the meta-information about the pm2 process by the command.
pm2 list
Obviously, we want to check logs as well, which can easily be checked by command pm2 logs, it will show all the logs for your running NodeJS application.
Second Method:
The second method revolves around running the service using the start script that you have configured in your node, package.json file.
Generally, the command which you use to run the service using the start script looks as follow
npm start
But now the challenge arrives, what you can do to run it with the pm2 process manager.
We can run it using the following command
pm2 start npm -- start
After successfully starting the process, you can check the meta-information about the pm2 process by the command.
pm2 list
Obviously, we want to check logs as well, which can easily be checked by the following command, it will show all the logs for your running NodeJS application.
pm2 logs
Forever
A simple CLI tool for ensuring that a given script runs continuously (i.e. forever). Link: https://www.npmjs.com/package/forever Forever is yet another tool with the feature to run the service as a daemon process. But personally, I have found forever has fewer features as compared to pm2. But it does the task of running your process as a daemon process very gracefully.
The first step is always to install the package.
npm install forever -g
Once your package is installed globally, check by typing forever and check if you get the basic help options shown. If not try installing it properly.
To start your service using forever, you can use the following command
forever start index.js
To check the list of the forever process running processes
forever list
To check the logs of any of the process, suppose you want to check logs of having uid 0
forever logs 0 -f
-f, can be used to keep following the logs. Otherwise, it will show the last few lines of logs and exist.
Hope now you must have got the idea about how you can start the NodeJS as a daemon process. It's simple until you are aware of it. This is something a must-have knowledge every developer should be aware of. These are few basic commands which will help you in making your service up and running, but I will highly recommend reading more about these tools. These tools can provide you with many features that can help you monitor your application better. Considering subscribing to our Newsletter to get more curated articles especially for you. Link https://mailchi.mp/341ebfcaf408/mindroast
Comments