Just define the env variable (e.g. NODE_ENV) on process start:
$ NODE_ENV='production' node server.js
If you use forever or pm2 to run your app, use this:
$ NODE_ENV='production' forever start server.js
$ NODE_ENV='production' pm2 start server.js
So you can access to this variable inside your app:
// Gets environment (default value: development)
var NODE_ENV = process.env.NODE_ENV || 'development';
Create a directory for your configuration files (e.g. config/), and create files for each of your environment (e.g. development.js, production.js, etc).
Here is an example of such file:
var config = module.exports = {};
// Any params you wish, incl. arrays and objects
config.port = 1337;
config.db = {
url: 'mongodb://...',
prefix: 'db_'
};
Now you can use specific configuration for your environment:
var NODE_ENV = process.env.NODE_ENV || 'development';
var config = require('/path/to/config/' + NODE_ENV);
console.log('Server port: ' + config.port);
You can put this into your main script and use dependency injection to provide configuration for your modules.
Other way is to create a simple config module.
Just create index.js inside config/ dir:
// Gets environment (default value: development)
var NODE_ENV = process.env.NODE_ENV || 'development';
// Returns specific config object
var config = module.exports = require('./' + NODE_ENV);
// Adds environment param
config.env = NODE_ENV;
Now we can rewrite the previous code of main script:
var config = require('/path/to/config');
console.log('Server port: ' + config.port);
Of cause, we don't want to create all configurations from scratch. Better way is to use inheritance and overriding.
Here is an example how to do this:
// Old version w/o inheritance
// var config = module.exports = {};
// Inherits development config
var config = module.exports = require('./development');
// Overrides some params
config.port = 3000;
config.data.greeting = 'Good morning';
That's all, it's too easy and obvious, but I hope it might be usefull for somebody :)