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
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).
Example for env.json:
In brief
This kind of a setup is simple and elegant :
env.json
common.js
app.js
To run in production mode :
$ NODE_ENV=production node app.js
In detail
This solution is from : http://himanshu.gilani.info/blog/2012/09/26/bootstraping-a-node-dot-js-app-for-dev-slash-prod-environment/, check it out for more detail.
An elegant way is to use
.env
file to locally override production settings. No need for command line switches. No need for all those commas and brackets in aconfig.json
file. See my answer hereExample: on my machine the
.env
file is this:My local
.env
overrides any environment variables. But on the staging or production servers (maybe they're on heroku.com) the environment variables are pre-set to stageNODE_ENV=stage
or productionNODE_ENV=prod
.A very useful solution is use the config module.
after install the module:
You could create a default.json configuration file. (you could use JSON or JS object using extension .json5 )
For example
This default configuration could be override by environment config file or a local config file for a local develop environment:
production.json could be:
development.json could be:
In your local PC you could have a local.json that override all environment, or you could have a specific local configuration as local-production.json or local-development.json.
The full list of load order.
Inside your App
In your app you only need to require config and the needed attribute.
Load the App
load the app using:
or setting the correct environment with forever or pm2
Forever:
PM2 (via shell):
PM2 (via .json):
process.json
And then
This solution is very clean and it makes easy set different config files for Production/Staging/Development environment and for local setting too.
How about doing this in a much more elegant way with nodejs-config module.
This module is able to set configuration environment based on your computer's name. After that when you request a configuration you will get environment specific value.
For example lets assume your have two development machines named pc1 and pc2 and a production machine named pc3. When ever you request configuration values in your code in pc1 or pc2 you must get "development" environment configuration and in pc3 you must get "production" environment configuration. This can be achieved like this:
Now create new config instance with following syntax.
Now you can get any configuration value without worrying about the environment like this:
My solution,
load the app using
Then setup
config.js
as a function rather than an objectThen 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.Then we can access the config object properties exactly as before