0

I would like to run a second node.js server (using same code) running from the same directory as the first. I use the default.json file in the config folder for the 1st server and use the config library, ie

let config = require('config');

for the first server.

What is the recommended way to specify an alternative config file. Could you specify a custom config file to use from the command line? How would I do this in node.js?

Angus Comber
  • 9,316
  • 14
  • 59
  • 107
  • Possible duplicate of [How do I pass command line arguments to a Node.js program?](https://stackoverflow.com/questions/4351521/how-do-i-pass-command-line-arguments-to-a-node-js-program) – Denys Séguret Jul 27 '18 at 09:08
  • At some point you call server.listen(port) try calling that a second time? – Lev Kuznetsov Jul 27 '18 at 09:13

1 Answers1

0

config is able to read different config files based on a few environmental variables. Since you'll be running multiple instances on the same system, you'll need to change NODE_APP_INSTANCE:

  1. Create config files like this:

    • config/default.json: Default configuration shared across all instances.
    • config/default-1.json: Specific configuration for instance 1. May override values of default.json and/or add new ones.
    • config/default-2.json: Specific configuration for instance 2. May override values of default.json and/or add new ones.
  2. Start your node processes with individual values for NODE_APP_INSTANCE:

    • Instance 1: NODE_APP_INSTANCE=1 node index.js
    • Instance 2: NODE_APP_INSTANCE=2 node index.js

Checkout the docs for more info.

Lucas S.
  • 2,303
  • 1
  • 14
  • 20