Skip to content

Instantly share code, notes, and snippets.

@hogart
Created November 4, 2014 05:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hogart/a18368eba492e70396cd to your computer and use it in GitHub Desktop.
Save hogart/a18368eba492e70396cd to your computer and use it in GitHub Desktop.
django-like settings for node projects

Put index.js to settings folder of your app, use like var settings = require('./settings'). Add as many environment specific files (development.js, testing.js, staging.js, production.js, etc) as you need. Use them from one another as you wish. Use local.js to override any option on given machine. Don't forget to include settings/local.js to .gitignore!

'use strict';
var env = process.env.NODE_ENV || 'development'; // development by default
var config = {};
try {
config = require('./local'); // local.js has priority
} catch (e) {
config = require('./' + env); // if local.js is not present, get config with environment name
}
module.exports = config;
'use strict';
// put this file in .gitignore, it is machine specific
var conf = require('./testing'); // inherit config from another
//override some settings
conf.godCredentials = { // simpler credentials for developer needs
name: 'admin',
pass: 'Qwerty123'
}
conf.port = 1337; // I ran app locally on any port I choose
conf.db = 'http://testing.application.com:16737'; // but I use testing db for now
module.exports = conf;
'use strict';
module.exports = {
// gettings settings from environment variables are useful when running under Dokku or similar deployment system
db: process.env.MONGO_URL, // mongo connection string
port: process.env.PORT, // port to run app on
godCredentials: { // credentials for back-office
name: '15045784',
pass: 'nUgjVnycrhuWyQPNuYlCnbJ4u'
},
// some API keys or whatever
twitterKey: '',
twiterSecret: ''
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment