Grofit
Grofit

Reputation: 18465

Building applications for different environments with nodejs

Historically on most large projects I have worked on we have a build script of some sort which is run by a developer to setup their environment, such as setting up iis, database migrations, templating configuration files etc.

As each environment is different, I would generally have a set of configuration files for each environment, such as dev.config, qa.config or release-candidate.config, then when the build process runs it would use the <environment>.config file to find out what its settings should be. This way my dev environment can build fine and the build server can do its stuff fine too in an automated fashion.

Anyway in .net we have configuration files such as web.config which can be templated, is there any notion of this within the nodejs world? as I want to have a file which contains all the configuration values required for the application to run without putting them in the application code.

Upvotes: 0

Views: 1763

Answers (2)

Alexander Beletsky
Alexander Beletsky

Reputation: 19841

You can do that very simple, actually. Just place all your configuration in .js file, like

{
  mongodb: {
    connection: 'http://mongodb.com/mydb:12323/user/password',
    options: {
      keepConnection: true
    }
  },

  app: {
    deploymentKey: '1234'
  },

  // etc
}

So, you will have 2-3 files, depending on the number of environments you have.

   /config/development.js
   /config/production.js
   /config/test.js
   /config/staging.js

The environment type is typically exposed by NODE_ENV variable. Then you have really simple module, to load the configuration:

var util = require('util');

var env = process.env.NODE_ENV || 'development';
var config = util.format('/%s.config.js', env);

module.exports = require(__dirname + config);

Checkout some real code here;

Upvotes: 2

Gabriel Llamas
Gabriel Llamas

Reputation: 18447

This is why I created the properties module.

Check the environment example.

Upvotes: 1

Related Questions