God
God

Reputation: 684

In nodeJS how to use database environment when NODE_ENV calls

hi am new to nodejs environment.

am using nodeJs + compoundjs.

am having three database environment development. production and test. my question in when i run the NODE_ENV=production node . command, all url's,port number and other things should get from production.js. when i shift the node environment by giving command NODE_ENV=development node . all things need to run should get from development.js.

any notes for this also helpful for me.

if anybody has any idea please share with me.

Upvotes: 0

Views: 1230

Answers (1)

pfried
pfried

Reputation: 5079

You have to set the Environment and then you can configure your app like:

(This is a mongoose db and express, but you can find similar configurations.)

Simply set up three environment configurations

app.configure('development', function () {
  mongoose.connect(devConfig.db.path, function onMongooseError(err) {
  });
});

app.configure('production', function () {
  mongoose.connect(proConfig.db.path, function onMongooseError(err) {
  });
});

a configuration example (config.js) :

var config = {};

// Database (MongoDB) configurations
config.db = {
path : 'mongodb://localhost/sampleDatabase'
};

module.exports = config;

I require this file in my app.js by var config = require('config')

You could do the Environment detection in the config file as well.

Upvotes: 1

Related Questions