user2321728
user2321728

Reputation: 1373

How to restart nodejs server in order to apply changes, when nodejs app is hosted through Apache

I use apache server to redirect to the NodeJS server on request of a specific domain. But when I add some functions to js code(server code), it says something like "not defined", even I tried to restart apache server, and killed all the node related processes.

What should I do now?

Upvotes: 0

Views: 834

Answers (1)

clay
clay

Reputation: 6017

Run your node server with forever. There are other monitoring libraries out there as well.

The idea is that when files change (which can be white-listed), the app will be restarted. You need this if passing traffic through Apache, as restarting Apache will not restart the Node app, which needs to restart to read in your changes.

I use a script like this to start my app with forever, and I use it during development with node start.js or in production with some environment variables to config my Node env, and an "on boot" service so it runs on machine start (I use 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 );
} );

Upvotes: 1

Related Questions