@rmarscher/config
v1.0.4
Published
Standard way to initialize a config object.
Downloads
2
Readme
Node Config
Returns a config object using environment vars. If a .env file exists at the root of the project, it will load that to set environment vars. Returns the nconf instance which has get/set/merge. https://github.com/indexzero/nconf
Here is the order of the merging done by nconf:
- Defaults
- Environment vars
- Memory
- Overrides
By default, only defaults are set and environment vars are retrieved.
The calling application can update the defaults, set settings to
the memory store using config.set()
or config.merge()
and
also set up overrides which will always take precedence over
memory or environment vars.
Note that with nconf, a colon - ':' - is the default key separator and it's possible to get/set deep into objects by using a colon in the key.
i.e.
config.get( 'mongo:uri' ); // 'mongodb://localhost/test'
Setup
It is expected to load this module before anything else in your app. It will setup New Relic monitoring if process.env.NEW_RELIC_HOME is set. Note that this must be an actual environment var and not part of a .env file because we want to be able to instrument even the dotenv module.
import config from '@rmarscher/config';
That config object is ready to go with our standard defaults and could be just used out of the box.
We recommend at least setting the app name for the log:
config.set( 'log:name', 'my-app' );
Another way to setup the config is to use the config.setup(defaults, overrides)
method. This will completely clear out all of the existing config and set it
up again. The defaults will be merged with the standard defaults.
Environment Vars
Use two underscores __
to nest paths and create sub-objects. So an envrionment var
database__host=127.0.0.1
would cause
require( '@rmarscher/config' ).get( 'database' )
to return { host: '127.0.0.1' }
.
It's expected to load this before anything else in the app. It will also
read environment variables in from a .env file using the dotenv
package.
One thing to be careful about with environment variables is everything is treated as a string. You can get a falsey value by setting the environment variable to an empty string:
express__cors=
Therefore, it's important that anything that consumes a config setting
handle casting it appropriately. For booleans, it's recommended to
use just an if ( config.get( 'somevar' ) )
so truthy/falsey values will work.
Setting express__cors=false
or express__cors=0
in a .env or environment var
will not evaluate to false. It's very important to remember this.
Similarly, numeric values could be strings if set from env vars, so you
must use something like parseInt()
or Number()
to make sure to
convert strings to numbers.
Standard Defaults
Here are the standard defaults currently in use.
The express
, newrelic
and port
config are required for the
@rmarscher/express-init
module. The log
config is required for
the @rmarscher/log
module.
The paths
config is used by the requireLocal()
function
in the @rmarscher/globals
module. Given requireLocal()
can't
work with ES6 modules, we may phase that out. Although, it
might be still fairly handy until we can figure out if the
ES6 import paths to check can be configured.
var standardDefaults = {
express: {
cors: false,
paths: {
'static': path.resolve( process.cwd(), './public' ),
'views': path.resolve( process.cwd(), './app/views' ),
},
trustProxy: true,
viewEngine: 'hbs'
},
log: {
file: null,
level: 'info',
name: 'log', // maybe there's a way to get the calling app's name from package.json
stream: process.stdout
},
mongo: {
uri: 'mongodb://localhost/test',
log: false,
options: {
db: {
safe: true
},
socketOptions: {
keepAlive: 1
}
}
},
newrelic: newrelic ? true : false,
paths: {
'code': path.resolve( process.cwd(), './app' ),
'public': path.resolve( process.cwd(), './public' ),
'tests': path.resolve( process.cwd(), './test' ),
},
port: process.env.PORT || 8080,
// NodeMailer SMTP Transport settings
smtp: {
// host: 'smtp.mandrillapp.com',
port: 587,
// auth: {
// user: '',
// pass: ''
// },
// default is 5, trying a higher value
'maxConnections': 15,
'maxMessages': Infinity
},
versionPrefix: '/v1'
};