The purpose of this post is to help you get started with tools for Node.js in general and Loopback specifically to ease you life as Developer and Operator.
You need a Node.js based API (to follow through this article. PM2 supports other languages too).
The PM2 Processmanager is a mighty one with many different options, has various integrations and even supports an online monitoring.
In this Article I present you the basic usage of it to get started fast. In a later Article I will help you migrate to Docker and add the online monitoring of your processes.
The Installation is as easy as
. It is important that you install it globally.
npm install pm2 -g
How to use it
You can manually start an api by issuing
, but then you have to specify all parameter on every start. So it could be fast to test, but I recommend writing a small configuration file for it name
pm2 start app.js
Most often my configuration looks like this:
apps: - script: path_to_startup_script name: api_name exec_mode: cluster instances: 1 env: DEBUG_FD: 1 DEBUG_COLORS: 1
is the root element.
entry refers to an app or api that should be started. Here you define the path to the file that starts your api.
You can either define one app per app/api or you can even define a stack you want to start with multiple entries.
Personally I use one configuration file per api in development and one configuration for a stack in the staging environment before I deploy to docker.
describe the name you will see in the process manager when you list the running processes.
and the following
things get interesting. If you define the mode fork you can only start up one instance of this app/api. But if you define cluster then you´re able to scale the app/api with one single command and pm2 will load balance it for you!
defines how many concurrent instances you want to launch of this app/api at startup. I set this normally to 1 and adjust on the fly according to the needs.
This way I can already get a first idea of the need to scale.
you can specify environment variables you want to set.
tells Node.js to change the output stream to process.stdout.
will add colors to the pm2 log output. This is handy because you see on the first glance if the logs message is an error or not.
These have not been all possibly Attributes for the configuration file. If you want a tighter control have a look at the Configuration File Attributes (2).
After this explanation you will find my configurations for a Express based API and a Loopback based API.
apps: - script: bin/www name: api exec_mode: cluster instances: 1 env: DEBUG_FD: 1 DEBUG_COLORS: 1 NODE_ENV: staging
apps: - script: server/server.js name: loopback_api exec_mode: cluster instances: 1 env: DEBUG_FD: 1 DEBUG_COLORS: 1
After we defined the configuration we need to interact with the Processmanager to start and stop our APIs and check the log output.
Listing currently running Processes
To view all currently running Processes issue
pm2 list at a console.
Start an API with an process.x
Starting your API with an configuration file is as easy as the command
(x is config.js, json or yml).
pm2 start process.x
After this command PM2 starts your API with the specified configuration and outputs it list of currently running processes.
Stopping an API
You can stop your API with
pm2 stop process.x
Important to know is that PM2 just stops your API then, but won´t remove it from the prepared to run process list. If you want to remove it cleanly and make sure on the next start you have a clean plate you have to destroy it.
Destroying an prepared to run API entry
To remove a API entry from the prepared to run list you issue
pm2 delete process.x
Check the Logs
To check all logs without filtering to one API you issue
If you want to filter the logs to one specific API you can add it to the command like this
pm2 logs name_of_your_api
If you have any question post them into the comments. Or feel free to send my an email.