This tutorial shows how to keep the node server alive continuously and also how to restart the node server after an error or crash.
forever npm library provides a CLI tool to keep node server continuously running.
It restarts a server if the node server stopped because of an error.
It provides the following ways to integrate into node application
- CLI tool
- Code
Nodejs forever server alive
First, install forever npm library into an application
In the terminal, run the below command
npm install forever -g
It installs forever CLI globally.
Here is a command to start the script and run it continuously
forever start main.js
you can see the output as follows process is started as a daemon process and runs in the background.
info: Running action: start
info: Forever processing file: main.js
You can see different options for forever command
.
type forever in the terminal to get available options and configurations.
forever
you can pass the options to the command line or copy them to a json file.
Create a dev.json file
{
"append": true,
"watch": true,
"script": "index.js"
}
then, you can run the below command as follows
forever start dev.json
You can also check for more about forever npm
You can also start forever using code.
It also provides start, stop,startall, restart,restartall, list all scripts.