Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js: How to setup different variables for prod and staging

I'm using Express and I need to use different credentials for each server (staging and production).

I could setup the variables in the server.coffee file but then I'd need to access those variables in different files.

server.coffee:

app.configure 'production', () ->
 app.use express.errorHandler()

What's the solution? Setup the variables and then export them?

like image 388
donald Avatar asked Jan 15 '12 02:01

donald


People also ask

Should you use .env in production?

Using environment variables is a somewhat common practice during Development but it is actually not a healthy practice to use with Production. While there are several reasons for this, one of the main reasons is that using environment variables can cause unexpected persistence of variable values.


1 Answers

./config.js

var development = {
  appAddress : '127.0.0.1:3000',
  socketPort : 4000,
  socketHost : '127.0.0.1',
  env : global.process.env.NODE_ENV || 'development'
};

var production = {
  appAddress : 'someDomain.com',
  socketPort : 4000,
  socketHost : '8.8.8.8',
  env : global.process.env.NODE_ENV || 'production'
};

exports.Config = global.process.env.NODE_ENV === 'production' ? production : development;

./app.js

var cfg = require('./config').Config;

if (cfg.something) { // switch on environment
  // your logic
}
like image 92
Pono Avatar answered Nov 15 '22 15:11

Pono