How to – Keep a Node.js Server Running (Install and Run Forever Module on Node.js)

If you have a web application running on Node.js (v0.8.21), probably you have encountered a situation where an exception occurs in your server side .js code.

What is unacceptable is that when an exception occurs, your Node server stops and your whole application fails. What we want is for the node server to restart after an exception and keep working.

For this there is a node.js module which is called “forever”. In this post I will be talking about how to install it, run your node.js server code with it and how to stop it.

Installing is pretty straight forward, you install forever like any other node.js module using npm.

npm install -g forever

Note: Don't forget the -g option here, forever wants to be installed globally.

After installing forever you can start your node server as below:

forever start <your node.js server code>

At first you might get an unexpected error message saying:

Error: ENOENT, no such file or directory 'c:root.forever6qtX.log'  1  

Error message is clear enough. Just create “root” folder in C, create “forever” folder under it and create 6qtX.log file in there.

Now run the “forever start <your node.js server code>” line again and it should be fine. Your node.js code will start in “forever mode”. (you might see node.exe prompt opening here, I think it is the indicator of your forever process, if you close it, you will see it re-open immediately.)

Now there is another problem when you want to stop your forever process.

When you type:

forever stop <your node.js server code>

you will see forever module complaining about how he can not find that process   2  , also when you type:

forever list

“no forever processes running” message will be presented to you.   3  

Seems like “forever” can not find the processes he is running, so he can not stop them neither. What you have to do is kill the process manually if you want it to stop.

Although there are errors to be solved I want to thank the guys for this useful module, it will be wonderful when it is complete.

Forever on github:
https://github.com/nodejitsu/forever

Hope this helps someone.

Good luck,
Serdar.

Your email address will not be published. Required fields are marked *