Skip to content
This repository has been archived by the owner on Oct 25, 2022. It is now read-only.

The simplest way to configuration inheritance in Node.js app

Notifications You must be signed in to change notification settings

dmitry-guryev/nodejs-config-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple Node.js app configuration pattern

How to define an environment?

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';

How to define and get specific configuration?

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);

And what about configuration inheritance?

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 :)

About

The simplest way to configuration inheritance in Node.js app

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published