Node.js setting up environment specific configs to be used with everyauth


I am using node.js + express.js + everyauth.js. I have moved all my everyauth logic into a module file

var login = require('./lib/everyauthLogin');

inside this I load my oAuth config file with the key/secret combinations:

var conf = require('./conf');
twitter: {
    consumerKey: 'ABC', 
    consumerSecret: '123'

These codes are different for different environments - development / staging / production as the callbacks are to different urls.

Qu. How do I set these in the environmental config to filter through all modules or can I pass the path directly into the module?

Set in env:

app.configure('development', function(){
  app.set('configPath', './confLocal');

app.configure('production', function(){
  app.set('configPath', './confProduction');

var conf = require(app.get('configPath'));

Pass in

app.configure('production', function(){
  var login = require('./lib/everyauthLogin', {configPath: './confProduction'});

? hope that makes sense

11/30/2011 8:12:52 PM

Accepted Answer

My solution,

load the app using

NODE_ENV=production node app.js

Then setup config.js as a function rather than an object

module.exports = function(){
        case 'development':
            return {dev setting};

        case 'production':
            return {prod settings};

            return {error or other settings};

Then as per Jans solution load the file and create a new instance which we could pass in a value if needed, in this case process.env.NODE_ENV is global so not needed.

var Config = require('./conf'),
    conf = new Config();

Then we can access the config object properties exactly as before

12/2/2011 1:04:27 PM

You could also have a JSON file with NODE_ENV as the top level. IMO, this is a better way to express configuration settings (as opposed to using a script that returns settings).

var config = require('./env.json')[process.env.NODE_ENV || 'development'];

Example for env.json:

    "development": {
        "MONGO_URI": "mongodb://localhost/test",
        "MONGO_OPTIONS": { "db": { "safe": true } }
    "production": {
        "MONGO_URI": "mongodb://localhost/production",
        "MONGO_OPTIONS": { "db": { "safe": true } }

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow