2015-01-12 33 views

回答

1

forever運行您的節點服務器。還有其他的監控庫。

這個想法是,當文件改變(可以是白名單),應用程序將重新啓動。如果通過Apache傳遞流量,則需要此操作,因爲重新啓動Apache將不會重新啓動節點應用程序,該應用程序需要重新啓動以讀取更改。

我用這樣的腳本來啓動我的應用程序forever,我在開發過程中使用它node start.js或在生產中使用一些環境變量來配置我的節點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); 
}); 
相關問題