简体   繁体   中英

How does one include Heroku environment variables using node-config?

Currently I'm using this pattern in code:

module.exports.getMySQL = () => {
  return process.env.CLEARDB_DATABASE_URL || config.get('MySQL').connection;
}

however, node-config claims to be able to integrate these variables into a file as such.

https://github.com/lorenwest/node-config/wiki/Environment-Variables#custom-environment-variables

{
  "Customer": {
    "dbConfig": {
      "host": "PROD_SERVER"
    },
    "credit": {
      "initialDays": "CR_ID"
    },
    // Environment variables containing multiple configs
    // New as of config@1.14.0
    "settings": {
      "adminAccounts": {
        "__name": "ADMIN_ACCS",
        "__format": "json"
      }
    }
  }
}

What exactly is "PROD_SERVER"

If I replace this with "process.env.SOME_ENVIRONMENT_VARIABLE" , it does not work and my server crashes.

I did verify that "process.env.SOME_ENVIRONMENT_VARIABLE" exists using the Heroku GUI.

PROD_SERVER can be a name of the database server that you are trying to connect, if you need to read it from the Heroku configuration you need to set the configurations in heroku.

Lets say you set an environment variable with the name "DATABASE_URL" in heroku you can access it in your code as process.env.DATABASE_URL.

The environment variables for your Heroku app will be available in the settings tab of your app. Under "Config Variables"

Your server crashes when you use process.env.SOME_ENVIRONMENT_VARIABLE because the value is not set in configuration, you can see your Heroku app's console for more details.

I'm one of the maintainers of node-config .

PROD_SERVER in the docs is an example of an environment variable name.

You would place SOME_ENVIRONMENT_VARIABLE in custom-environment-variables.json , but don't put process.env.SOME_ENVIRONMENT_VARIABLE in the JSON file.

From the example custom-environment-variables.json file you've provided, you could then use config.get('Customer.dbConfig.host') and this would reference the value you set in the PROD_SERVER environment variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM