lib/cligen.js
function runWebServerAsWorker(rawConfig, serverConfig, emitter, callback) {
  if (serverConfig.command == 'stop') {
    logger.info('stopping server workers');
    if (netServer) {
      netServer.close();
      netServer = null;
    }    
    
    for (var i=0;i<webservers.length;i++) {
      var webserver = webservers[i];
      webserver.kill();    
    }
    webservers = [];
    
    return;
  }
  
  netServer = require('net').createServer().listen(serverConfig.options.port, function() {
    var numWorkers = require('os').cpus().length;
    var readyCount = 0;
    for (var i=0;i<numWorkers;i++) {
      var webserver = fork(__dirname + '/worker.js', [], {env: process.env});
      webservers.push(webserver);
      webserver.send({message: 'start', rawConfig: rawConfig, serverConfig: serverConfig}, netServer._handle)
      
      webserver.on('message', function(m) {
        if (m == 'ready') {
          //this is a reply from the worker indicating that the server is ready.
          readyCount += 1;
          if (readyCount == numWorkers) {
            //all servers are ready
            logger.info('started server workers');
            if (callback) callback();
          }
          return;
        }
        
        var webserverWorker = this;
        var id = m.id;
        if (m.err) { 
          logger.error("Error: " + m.err);
          if (callback) callback(m.err);
        } else {
          emitter.emit(m.commandName, m.config, function(err, result) {
            webserverWorker.send({message: 'result', err: err, result: result, id: id})
          });
        }
      })
    }
    
    
  });
}
function runWebServerAsWorker(rawConfig, serverConfig, emitter, callback) {
  if (serverConfig.command == 'stop') {
    logger.info('stopping server workers');
    if (netServer) {
      netServer.close();
      netServer = null;
    }    
    
    for (var i=0;i<webservers.length;i++) {
      var webserver = webservers[i];
      webserver.kill();    
    }
    webservers = [];
    
    return;
  }
  
  netServer = require('net').createServer().listen(serverConfig.options.port, function() {
    var numWorkers = require('os').cpus().length;
    var readyCount = 0;
    for (var i=0;i<numWorkers;i++) {
      var webserver = fork(__dirname + '/worker.js', [], {env: process.env});
      webservers.push(webserver);
      webserver.send({message: 'start', rawConfig: rawConfig, serverConfig: serverConfig}, netServer._handle)
      
      webserver.on('message', function(m) {
        if (m == 'ready') {
          //this is a reply from the worker indicating that the server is ready.
          readyCount += 1;
          if (readyCount == numWorkers) {
            //all servers are ready
            logger.info('started server workers');
            if (callback) callback();
          }
          return;
        }
        
        var webserverWorker = this;
        var id = m.id;
        if (m.err) { 
          logger.error("Error: " + m.err);
          if (callback) callback(m.err);
        } else {
          emitter.emit(m.commandName, m.config, function(err, result) {
            webserverWorker.send({message: 'result', err: err, result: result, id: id})
          });
        }
      })
    }
    
    
  });
}
function runWebServerAsWorker ( rawConfig , serverConfig , emitter , callback ) { if ( serverConfig . command == 'stop' ) { logger . info ( 'stopping server workers' ) ; if ( netServer ) { netServer . close ( ) ; netServer = null ; } for ( var i = 0 ; i < webservers . length ; i ++ ) { var webserver = webservers [ i ] ; webserver . kill ( ) ; } webservers = [ ] ; return ; } netServer = require ( 'net' ) . createServer ( ) . listen ( serverConfig . options . port , function ( ) { var numWorkers = require ( 'os' ) . cpus ( ) . length ; var readyCount = 0 ; for ( var i = 0 ; i < numWorkers ; i ++ ) { var webserver = fork ( __dirname + '/worker.js' , [ ] , { env : process . env } ) ; webservers . push ( webserver ) ; webserver . send ( { message : 'start' , rawConfig : rawConfig , serverConfig : serverConfig } , netServer . _handle ) webserver . on ( 'message' , function ( m ) { if ( m == 'ready' ) { //this is a reply from the worker indicating that the server is ready. readyCount += 1 ; if ( readyCount == numWorkers ) { //all servers are ready logger . info ( 'started server workers' ) ; if ( callback ) callback ( ) ; } return ; } var webserverWorker = this ; var id = m . id ; if ( m . err ) { logger . error ( "Error: " + m . err ) ; if ( callback ) callback ( m . err ) ; } else { emitter . emit ( m . commandName , m . config , function ( err , result ) { webserverWorker . send ( { message : 'result' , err : err , result : result , id : id } ) } ) ; } } ) } } ) ; }
Runs the web server in a forked process.  This is not necessarily quicker, but it does allow the
user's code to hog CPU without affecting the running of the web server
Runs the web server in a forked process . This is not necessarily quicker but it does allow the user s code to hog CPU without affecting the running of the web server