如何使用Express永远保持NodeJS服务器运行?

时间:2014-08-01 16:08:21

标签: node.js express forever

我有一个Express NodeJS服务器,我在项目根文件夹中通过终端npm start手动启动。我全局下载并安装了Forever软件包。当我使用:

对我的app.js文件运行Forever时
forever start app.js

我的服务器无法启动。我假设这是因为createServer文件中没有明确的app.js命令。我应该针对forever start命令运行什么文件来启动我的服务器?

4 个答案:

答案 0 :(得分:5)

在我的节点服务器上,我使用npm forever

sudo forever start app.js

请注意,您需要sudo

答案 1 :(得分:4)

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

答案 2 :(得分:2)

首先,我创建了一个Upstart脚本。我在Amazon EC2 AMI上运行,但是对于其他操作系统,还有其他类似的工具。

# 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

然后我使用了一个start.js文件来“托管”我的应用。我的真实应用位于index.js。您可以跳过底部的process.on内容,但我喜欢它。

/*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 );
} );

适合我!如果你只是想看到你的应用程序继续运行,你可以跳过新手的东西 - 这是我的生产解决方案。

答案 3 :(得分:0)

forever -w ./bin/www 

在项目文件夹中,运行命令forever -w ./bin/www。它对我有用。我相信它会对你有用。