JSON (config file) with dynamic variables

2k views Asked by At

I'm using a node package called 'db-migrate' and it uses a database.json file as a databases configuration. The problem is we are starting to use Docker and I want to set the values as environment variables with fallback - for example:

{
 ...
 "host": process.env.DBHOST || 'localhost',
 ...
}

But how can I do it with a static json file? I would like to use something similar to erb file with the dynamic abilities of template generation.

1

There are 1 answers

1
suraj On BEST ANSWER

According to my understanding, better idea is to create a global config file like config.js, and put your configurations like

module.exports = {
    // APP SETTINGS
   ...
   "host": process.env.DBHOST || 'localhost',
   "port": process.env.DBPORT || '8080',
   ...
}; 

After doing this, you can easily access this file wherever you want. For e.g. if you want to access these configurations in app.js file, then simply include it by adding a line in app.js file

var config = require('./config');

This will make the values available under config namespace and the values can be accessed as :

config.host or config.port

Hope the answer tells what you wanted.. If your intention is something else then please comment..