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

后端 未结 5 1016
说谎
说谎 2021-02-04 12:05

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

5条回答
  •  闹比i
    闹比i (楼主)
    2021-02-04 13:09

    ./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
    }
    

提交回复
热议问题