Chovy's Blog

Managing config variables inside a Node.js application

In the interest of avoiding debate over this post, let’s just say there is more than one way to skin a cat when it comes to configuration management in an application. Some like to keep it out of the code base, others prefer it. This method stores the config files in the repository.

There is more than one way to handle configuration variables based on an environment for a node.js app, but I’ve come up with a pretty simple solution that keeps my config variables DRY and complete control to override them at the same time using different node environments.

The basic premise is to set a NODE_ENV environment variable before you start your node app, then use that variable to load a matching config.whatever.js file. So if you start your app with
NODE_ENV=test node./app.js then you will load config.test.js file that overrides a config.global.js file….more on that in a second.

Here’s the basic config/index.js file, this will load the config.test.js file assuming your
NODE_ENV=test (we will default to ‘development’ if NODE_ENV is not defined):

JavaScript

1

2

3

4

varenv=process.env.NODE_ENV||'development'

,cfg=require('./config.'+env);

module.exports=cfg;

Next comes the config.test.js which will include config.global.js and then overwrite it’s json objects as needed:

config.test.js:

JavaScript

1

2

3

4

5

6

7

varconfig=require('./config.global');

config.env='test';

config.hostname='test.example';

config.mongo.db='example_test';

module.exports=config;

And the config.global.js which defines all the defaults:

JavaScript

1

2

3

4

5

6

7

8

9

varconfig=module.exports={};

config.env='development';

config.hostname='dev.example.com';

//mongo database

config.mongo={};

config.mongo.uri=process.env.MONGO_URI||'localhost';

config.mongo.db='example_dev';

Now we wrap it all together and use it in our code…for example in a model, you might do something like this in ./models/user.js:

JavaScript

1

2

3

varmongoose=require('mongoose')

,cfg=require('../config')

,db=mongoose.createConnection(cfg.mongo.uri,cfg.mongo.db);

And that’s all there is to it.
You just need need to be sure you define all your defaults in
./config/config.global.js and overwrite based on the environment in
./config/config.<env>.js .

Be sure to start your app with any of the following (I usually have test, production and development environments):

JavaScript

1

2

3

NODE_ENV=test node./app.js

NODE_ENV=development node./app.js

NODE_ENV=production node./app.js

Related Posts:

Testing a node.js express app with Mocha and Should.js I've been doing more Test Driven Development the past few months with my node.js application code base. With my latest project Wishd.me, I decided to get started early with writing tests. While skeptical at first, I have to admit it does save quite a bit of effort in debugging and…

Restart a Node.js app when the server reboots I had some issues when I first setup my Debian VPS for my node.js app over at Wishd.me when the ISP rebooted my server a couple of times unexpectedly and my node app didn't come back online. I didn't find many options out there, so I wrote a node-startup script…

Backing up and restoring a mongodb to a different database I'm using mongodb as my NoSQL database of choice for Wishd.me. I needed a quick way to backup the mongo database to another server, and then load that production database in a test environment locally so I could do some testing. Here is how I backed up mongodb using mongodump…

chovysblog

People have different ideas on how configuration management should be handled. Here’s an example from Eric Elliot on Geekli.st — he suggested not checking environment data into your repository and instead loading a shell script that has all the sensitive info like db hostnames and passwords.