Created
January 15, 2012 17:54
-
-
Save jessefulton/1616583 to your computer and use it in GitHub Desktop.
Node.js - merge environment variables + config file + command line args
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Merges environment variables + config file + command line args | |
* | |
* To use, from app.js: | |
* var conf = require('./config').init(); | |
*/ | |
var argv = require('optimist').argv; | |
function init() { | |
//1. check process.env | |
//2. config files override | |
//3. command line args final override | |
var vars = [ | |
"GOOGLE_API_KEY", | |
"GOOGLE_CSE_ID", | |
"MONGODB_URI", | |
"LOGGLY_SUBDOMAIN", | |
"LOGGLY_INPUT_KEY" | |
]; | |
var config = {}; | |
var fileConfig = {}; | |
try { | |
fileConfig = require('./conf/config.js'); | |
} catch(e) {} | |
vars.forEach(function(el, idx, arr) { | |
config[el] = process.env[el]; | |
if (fileConfig[el]) { config[el] = fileConfig[el]; } | |
if (argv[el]) { config[el] = argv[el]; } | |
}); | |
return config; | |
} | |
exports.init = init; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Fill in your accounts below and place in ./conf/config.js (see fileConfig var in config.js) | |
module.exports = { | |
GOOGLE_API_KEY: "" | |
, GOOGLE_CSE_ID: "" | |
, MONGODB_URI: "" | |
, LOGGLY_SUBDOMAIN: "" | |
, LOGGLY_INPUT_KEY: "" | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment