简体   繁体   中英

How do I Use Forever With Express to Keep a NodeJS Server Running?

I have an Express NodeJS server that I manually start through terminal with npm start in my project root folder. I downloaded and installed the Forever package globally. When I run Forever against my app.js file using:

forever start app.js

my server doesn't start. I am assuming this is because there is no explicit createServer command in the app.js file. What file should I run against the forever start command to start my server?

On my node server, I use npm forever by:

sudo forever start app.js

Notice you need to sudo it

您只需要在项目文件夹中运行命令forever start ./bin/www ,一切都会好的:)

First, I create an Upstart script. I'm running on Amazon EC2 AMI, but there are other tools like this for other OSes.

# This is an upstart (http://upstart.ubuntu.com/) script
# to run the node.js server on system boot and make it
# manageable with commands such as
# 'start app' and 'stop app'
#
# This script is to be placed in /etc/init to work with upstart.
#
# Internally the 'initctl' command is used to manage:
# initctl help
# initctl status node-app
# initctl reload node-app
# initctl start node-app

description "node.js forever server for app"

#node child process might not really fork, so don't except it
#expect fork

# used to be: start on startup
# until we found some mounts weren't ready yet while booting:

start on runlevel [2345]
stop on runlevel [016]

# Automatically Respawn:
respawn
respawn limit 99 5

chdir /path/to/directory/node-app

exec node start.js

#post-start script
#   # Optionally put a script here that will notifiy you node has (re)started
#   # /root/bin/hoptoad.sh "node.js has started!"
#end script

Then I used a start.js file that "hosts" my app. My real app is located in index.js . You could skip the process.on stuff at the bottom, but I like it there.

/*jslint node: true */
"use strict";

/**
 * File to start using forever, logs crashes, restarts on file changes, etc.
 */

var cmd = ( process.env.DBG ? "node --debug" : "node" );

var forever = require( 'forever' ),
  //exec = require('child_process').exec,
  child = new( forever.Monitor )( 'index.js', {
    'silent': false,
    'pidFile': 'pids/node-app.pid',
    'watch': true,
    'command': cmd,
    //"max" : 10,
    'watchDirectory': './lib', // Top-level directory to watch from.
    'watchIgnoreDotFiles': true, // whether to ignore dot files
    'watchIgnorePatterns': [], // array of glob patterns to ignore, merged with contents of watchDirectory + '/.foreverignore' file
    'logFile': 'logs/forever.log', // Path to log output from forever process (when daemonized)
    //'outFile': 'logs/forever.out', // Path to log output from child stdout
    'errFile': 'logs/forever.err'
  } );

child.on( "exit", function() {
  console.log( 'node-app has exited!' );
} );
child.on( "restart", function() {
  console.log( 'node-app has restarted.' );
} );


child.start();
forever.startServer( child );

process.on( 'SIGINT', function() {
  console.log( "\nGracefully shutting down \'node forever\' from SIGINT (Ctrl-C)" );
  // some other closing procedures go here
  process.exit();
} );

process.on( 'exit', function() {
  console.log( 'About to exit \'node forever\' process.' );
} );

process.on( 'uncaughtException', function( err ) {
  console.log( 'Caught exception in \'node forever\': ' + err );
} );

Works for me! You can skip the upstart stuff if you just want to see your app keep running - this is my production solution.

forever -w ./bin/www 

In your project folder, run the command forever -w ./bin/www . It worked for me. I am sure it will work for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM