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

后端 未结 5 1010
说谎
说谎 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"));
    
    0 讨论(0)
  • 2021-02-04 12:44

    If you don't want the logic for determining which config to use in each file (which would look pretty ugly), you'll have to export it somewhere.

    What I would suggest: Have a config.json file containing the different configs. The main file requires it and does something like config.default = config.(condition ? 'production':'development'). In all other files, you can now just do require('./config').default.

    0 讨论(0)
  • 2021-02-04 12:48

    This might be a good place to use npm-config.

    When running scripts (see npm-scripts(7)) the package.json "config" keys are overwritten in the environment if there is a config param of <name>[@<version>]:<key>

    I would not use them for every type of variable configuration setting, but I think it's a good solution for simple cases like URLs and ports because:

    1. You put them directly into package.json.
    2. In addition, you can specify them on the command line or as ENV variables.
    3. Anything run through npm can refer to them (e.g., scripts).
    4. You can set them per-user with `npm config set foo:port 80

    The one caveat is that the config parameter in your package.json is only automatically exported into the environment when you run your code through npm. So, if you just run it with node, like, node ./myapp.js, then you can't expect that process.env.npm_package_config_foo will contain your value. However, you can always var pkg = require('./package.json'); and access the values at pkg.config.

    Because it might not be immediately obvious, I'd also add that the npm_package_config environment variables do not bubble up to apps that depend on your npm package. So, if your depended-on package refers to process.env.npm_package_config_foo, then the dependent package would have to define that in its own package.json. I guess since it's an "npm_package_config" it wouldn't make sense to push them all the way up the tree.

    So, how would I use one npm config variable and have it work all the way up the tree, in both the base package and the packages that depend on it? It's actually a little confusing, and I had to figure this out through trial and error.

    Let's say you have a package connector and package client. client depends on connector and you want to specify a config parameter for connector that can be used or overwritten in client. If you use process.env.npm_package_config.port in your connector module, then when it's depended on in client module, then that variable won't be exported and it will end up as undefined.

    However, if you instead use process.env.npm_config_connector_port (notice the first one starts with npm_package_config and the other with npm_config_packagename), then you can at least set that in your .npmrc using npm config set connector:port 80 and it will be "namespaced" as process.env.npm__config_connector_port everywhere that you run npm, including scripts that you run in client that depend on connector, and you'll always be able to overwrite it on the command line, in your ENV, or in your .npmrc. You just have to keep in mind that, as with any environment variable, it may not always be set. So, I would use the default operator with the process.env.npm_config_connector_port as the first (preferred) value:

    var port = process.env.npm_config_connector_port || sane_default

    Here, sane_default could be populated from one of the other recommended methods. Personally, I like keeping configuration data like these in JSON files at the very least, and package.json seems like the best JSON file to put them in. Store them in data instead of code and then you can easily use the static JSON in-line, generate them dynamically, or pull them from the filesystem, URLs or databases.

    0 讨论(0)
  • 2021-02-04 12:54

    I have an app which uses three different methods of declaring config variables (uris, api keys, credentials, etc.) depending upon the environment (production = environment variables; staging = command line args; local = config files.) I wrote a little "config" module to handle merging all of these options into one object that I can use in my app and uploaded it as a gist: https://gist.github.com/1616583

    It might not be the best implementation, but it's been working pretty well so far :).

    0 讨论(0)
  • 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
    }
    
    0 讨论(0)
提交回复
热议问题