title: NodeJS - Passing command line parameters to a required module
body: <p>This is my folder structure for an ExpressJS application that exposes a couple of APIs.</p>
  
  <pre><code>/
  |_ index.js
  |_ app
     |_ server.js
  |_routes
    |_route1.js
  </code></pre>
  
  <p>I start the application with <code>pm2</code> as:
  <code>pm2 start index.js --name server1 -- $HOME/cfg/config.json</code></p>
  
  <p>In <code>index.js</code>, I have <code>require(./app/server)</code> where <code>server.js</code> needs a few parameters from <code>$HOME/cfg/config.json</code>. Since the configuration file is passed as arguments to <code>index.js</code>, I am not sure, how to pass the parameters to <code>server.js</code>. Can you please advise?</p>
  
  <p><code>index.js</code></p>
  
  <pre><code>var fs = require('fs')
  var app = require('./app/server');
  var http = require('http');
  //
  var launchServer = function(err, config) {
    server = http.createServer(app);
    server.listen(config.port);
    server.on('error', onError);
    server.on('listening', onListening);
  }
  //
  function onError(error) {
    if (error.syscall !== 'listen') {
      throw error;
    }
  
    var bind = typeof port === 'string'
      ? 'Pipe ' + port
      : 'Port ' + port;
  
    // handle specific listen errors with friendly messages
    switch (error.code) {
      case 'EACCES':
        console.error(bind + ' requires elevated privileges');
        process.exit(1);
        break;
      case 'EADDRINUSE':
        console.error(bind + ' is already in use');
        process.exit(1);
        break;
      default:
        throw error;
    }
  }
  //
  function onListening() {
   /** 
  
   How to pass server object ??
  
  
    var addr = server.address();
    var bind = typeof addr === 'string'
      ? 'pipe ' + addr
      : 'port ' + addr.port;
    debug('Listening on ' + bind);
  */
    console.log('Server started.')
  }
  //
  var getConfig = function(launchServer, configFile) {
    fs.readFile(configFile, function(err,data) {
      if (err) {
        return err;
      }
      launchServer(null, JSON.parse(data))
    })
  }
  //
  getConfig(launchServer, process.argv[2])
  </code></pre>