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

后端 未结 5 1009
说谎
说谎 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条回答
  •  隐瞒了意图╮
    2021-02-04 12:44

    I have uploaded an implementation into https://github.com/qiangyu/nodejsconfig. I believe it will satisfy your needs. Basically, you only need to provide one configuration file:

    dev.appAddress = '127.0.0.1:3000';
    prod.appAddress = 'someDomain.com';
    

    Then, you use following code to read the appAddress in prod environments:

    var xnconfig = require('nodejsconfig');
    var fs = require('fs');
    
    var data = fs.readFileSync(__dirname+"/config.properties", "UTF8");
    
    // assume we will be using environment "prod"
    var config = xnconfig.parse("prod", data);
    
    // the output will be someDomain.com
    console.log(config.getConfig("appAddress"));
    

提交回复
热议问题