What is going on guys? Today I want to show you the problem I encounter running the server node js on Windows 10 and how I manage to solve it.
I came across some errors after running node index_servo.js
for more than 24 hours, the reason was it run out of memory capacity for taking it too long. One solution was regularly restarting the program however I was thinking of making an automation by restarting whenever the same error appear. PM2 was brought to my attention after seeking help.
What is PM2 in a nutshell?
PM2 is a daemon process manager that will help you manage and keep your application online. Getting started with PM2 is straightforward, it is offered as a simple and intuitive CLI, installable via NPM.
Source
This is the solution I was thinking until my brother @eastmael named it to ease my problem as it will help us manage and keep our application online. I am running the program on Windows 10, if you are solving the same problem maybe this post will be helpful to you
How I did manage to install it?
First, start the command prompt by running it with admin permission then install the pm2 global by running this code
$ npm install pm2 -g
The return is;
Then we need to add a script on package json file on your program by adding this
"scripts": { "start": "node index_servo.js", },
After that, we can start the program by
$ pm2 start <appname>
but in my case I run this $ pm2 start index_servo.js
The return is;
It will help your to keep your application online and continue running in the background even if you close the command prompt.
If you still need option, these are some option you can pass to the CLI
Specify an app name
--name <app_name>
Watch and Restart app when files change
--watch
Set memory threshold for app reload
--max-memory-restart <200MB>
Specify log file
--log <log_path>
Pass extra arguments to the script
-- arg1 arg2 arg3
Delay between automatic restarts
--restart-delay
Prefix logs with time
--time
Do not auto restart app
--no-autorestart
Specify cron for forced restart
--cron <cron_pattern>
Attach to application log
--no-daemon
From time to time I can now check it on the command prompt by typing
$ pm2 status
It will show the status if its online or not, the uptime and the memory.
There are managing processes by using these commands
$ pm2 restart app_name
$ pm2 reload app_name
$ pm2 stop app_name
$ pm2 delete app_name
Before I end this post, the logs are also useful to look at. We can check the logs by typing
$ pm2 logs
This is my return
You can also check this for more information about pm2.
If you found this post useful, I will appreciate your reblog or retweet to help more people by keeping their application online.