How to Run Node.js as Background by Using Forever

2014/4/303 min read
bookmark this
Responsive image

This is for running at windows, I'm able to make it start but can't stop. Still looking for the solution. However, quick solution is, deploy your node.js web application to linux, seems like forever works. If you use windows for your deployment, how about try IIS-node.

When get start using node.js, you'll notice when you stop the node.js command, your HTTP server is done. You might close the command by your perpose, or after a long time, the machine auto closed command. At this kind of case, your node.js Http Server will not run automatically. So, In this blog, I'll show how to use forever.

I searched online, look like lots of people using is this  forever module created by nodejitsu. I have referenced their document about how to use  foreverkeep-a-nodejs-server-up-with-forever. I copy their server side code to my local machine and try to run in following enviroment.

 

  • windows server 2008 (64 bit)
  • node version : 0.10.26
  • npm 1.4.3
  • run as administrator

First try, it didn't work, I got different kind of error. Like this error at this page forever issue. So I tried following things.

 

  • Delete all file from npm folder, under /Users/{yourname}/AppData/Roaming/
  • Remove all files from except my server.js.

Try again, still not working, I tried following command, all not working. 

forever server.js
forever start server.js
forever node server.js



But this time, I saw the error is looking for following file, I thought, this's Linux, but looks like forever is looking for this file. Then I create manually but add folder root, and folder .forever under the root, and finally add the log file, but actually you don't need to add the log file. just the folder /root/.forever/ need to be exist.

C:/root/.forever/KsIJ.log

after I setup all these, and type. This time eventhough I close my command, the side is still running. Looks like it start a node.exe as background.

forever start server.js

following is the server.js code.

 

var util = require('util'),  
    http = require('http');

http.createServer(function (req, res) {  
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.write('hello')
  res.end();
}).listen(8000);

/* server started */  
util.puts('> hello running on port 8000'); 

Folder Structure when run ok

 

Inclusion

 

If you looked following post, you'll notice forever doesn't work on windows well. I have the same situation, able to start with forever but can't stop and list. So, there's no point of use it.

Forever windows issue 

following are few links about forever 

run ghost with forever

forever on github

forever issue

 

Looks like forever might working on windows now (5/22/2014)

Forever working on Windows

 

If you don't like forever, there are other choice you can use for same functions, however looks like they don't suppor windows either. Give up windows.