简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM