当nodejs app通过Apache托管时,如何重新启动nodejs服务器以应用更改

时间:2015-01-12 09:25:16

标签: node.js apache

我根据特定域的请求使用apache服务器重定向到NodeJS服务器。但是当我向js代码(服务器代码)添加一些函数时,它会说"未定义",甚至我试图重启apache服务器,并杀死所有与节点相关的进程。

我现在该怎么办?

1 个答案:

答案 0 :(得分:1)

使用forever运行您的节点服务器。还有其他监控库。

这个想法是当文件发生变化时(可以列入白名单),应用程序将重新启动。如果通过Apache传递流量,则需要这样做,因为重新启动Apache将不会重新启动Node应用程序,该应用程序需要重新启动才能读取您的更改。

我使用这样的脚本用forever启动我的应用程序,并在开发过程中使用node start.js或在生产中使用一些环境变量来配置我的Node env,并在“启动时” “服务所以它在机器启动时运行(我使用upstart)。

/*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 )( 'node', {
    'silent': false,
    'pidFile': 'pids/forever-app.pid',
    'watch': true,
    'command': cmd,
    'args': ['app.js' ],
    //"max" : 10,
    'watchDirectory': './', // Top-level directory to watch from.
    'watchIgnoreDotFiles': true, // whether to ignore dot files
    'watchIgnorePatterns': [ 'log/*', 'node_modules/*', 'pids/*',
                              'dbscripts/*', 'test/*',
                              'curlcookies',
                              '.svn/*', ], // array of glob patterns to ignore, merged with contents of watchDirectory + '/.foreverignore' file
    'logFile': 'log/forever.log', // Path to log output from forever process (when daemonized)
    //'outFile': 'logs/ijoin-forever.out', // Path to log output from child stdout
    'errFile': 'log/forever.err'
  } );

child.on( "exit", function() {
  console.log( 'app.js has exited!' );
} );
child.on( "restart", function() {
  console.log( 'app.js has restarted.' );
} );
child.on( 'watch:restart', function( info ) {
  console.error( 'Restaring script because ' + info.file + ' changed' );
} );

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