How to Run Node.js as Background by Using Forever

2014/04/303 min read
bookmark this
Responsive image

Table of Contents

  1. Introduction
  2. Getting Started
  3. Troubleshooting
  4. Running Forever Successfully
  5. Sample Server Code
  6. Conclusion

Introduction

This is for running on Windows. I was able to make it start but couldn't stop it. I'm still looking for the solution. However, a quick solution is to deploy your Node.js web application to Linux, where forever seems to work. If you use Windows for your deployment, you might want to try IIS-node instead.

When getting started with Node.js, you'll notice that when you stop the Node.js command, your HTTP server stops. You might close the command on purpose, or after a long time, the machine may auto-close the command. In these cases, your Node.js HTTP server will not run automatically. So, in this blog, I'll show how to use forever.

Getting Started

I searched online, and it looks like lots of people are using the forever module created by nodejitsu. I have referenced their document about how to use forever: keep-a-nodejs-server-up-with-forever. I copied their server-side code to my local machine and tried to run it in the following environment:

  • Windows Server 2008 (64-bit)
  • Node version: 0.10.26
  • npm 1.4.3
  • Run as administrator

Troubleshooting

First try: it didn't work. I got different kinds of errors, like the error on this page: forever issue. So I tried the following things:

  1. Deleted all files from the npm folder, under /Users/{yourname}/AppData/Roaming/

  2. Removed all files except my server.js

  3. Tried again, still not working. I tried the following commands, all not working:

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

But this time, I saw the error was looking for the following file. I thought this was a Linux thing, but it looks like forever is looking for this file. Then I manually created the folder root, added the folder .forever under root, and finally added the log file. Actually, you don't need to add the log file — just the folder /root/.forever/ needs to exist.

C:/root/.forever/KsIJ.log

Running Forever Successfully

After I set up all of these and typed the command below, this time even though I closed my command window, the site was still running. It looks like it starts a node.exe as a background process.

forever start server.js


Sample Server Code

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 running correctly.

Conclusion

If you look at the following post, you'll notice forever doesn't work well on Windows. I had the same situation — able to start with forever but couldn't stop or list processes. So, there's no point in using it on Windows. Consider deploying to Linux or using IIS-node on Windows instead.

Forever Windows issue

Following are a few links about forever:

  • Run Ghost with forever