Tawney
Tawney

Reputation: 13

database(mongoose) configuration for production in node

i am new to nodejs. I want to set up database(mongoose) configuration for production in a separate file same as we use properties file in java. Please help me with some example.

Upvotes: 1

Views: 1335

Answers (1)

williamC
williamC

Reputation: 176

I recommend using node-config

as best practice, you will have to create a config folder with config files corresponding to your NODE_ENV

for example, if you have three env:

  • development: local development host
  • production: production host
  • staging: staging host

you'll have to create 3 files plus a default.js config file, node-config will override default settings with the config file corresponding to NODE_ENV

config/default.js

module.exports = {
  // node port
  port: 3000
  // other default settings
};

config/development.js

module.exports = {
  // mongo url
  mongoUrl: "mongodb://localhost/db"
};

config/production.js

module.exports = {
  // mongo url
  mongoUrl: "mongodb://production.mongo.host/db"
};

config/staging.js

module.exports = {
  // mongo url
  mongoUrl: "mongodb://staging.mongo.host/db"
};

Upvotes: 1

Related Questions