简体   繁体   English

如何使用node-config包含Heroku环境变量?

[英]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. 但是,node-config声称能够将这些变量本身集成到文件中。

https://github.com/lorenwest/node-config/wiki/Environment-Variables#custom-environment-variables 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" 到底是什么"PROD_SERVER"

If I replace this with "process.env.SOME_ENVIRONMENT_VARIABLE" , it does not work and my server crashes. 如果将其替换为"process.env.SOME_ENVIRONMENT_VARIABLE" ,它将无法正常工作,并且服务器将崩溃。

I did verify that "process.env.SOME_ENVIRONMENT_VARIABLE" exists using the Heroku GUI. 我确实使用Heroku GUI验证了"process.env.SOME_ENVIRONMENT_VARIABLE"存在。

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. PROD_SERVER可以是您尝试连接的数据库服务器的名称,如果需要从Heroku配置中读取它,则需要在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. 假设您在heroku中设置了一个名称为“ DATABASE_URL”的环境变量,则可以在您的代码中以process.env.DATABASE_URL的形式访问它。

The environment variables for your Heroku app will be available in the settings tab of your app. 您的Heroku应用程序的环境变量将在您应用程序的设置选项卡中可用。 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. 当您使用process.env.SOME_ENVIRONMENT_VARIABLE时,服务器会崩溃,因为未在配置中设置该值,您可以查看Heroku应用程序的控制台以获取更多详细信息。

I'm one of the maintainers of node-config . 我是node-config的维护者之一。

PROD_SERVER in the docs is an example of an environment variable name. PROD_SERVER中的PROD_SERVER是环境变量名称的示例。

You would place SOME_ENVIRONMENT_VARIABLE in custom-environment-variables.json , but don't put process.env.SOME_ENVIRONMENT_VARIABLE in the JSON file. 您可以将SOME_ENVIRONMENT_VARIABLE放在custom-environment-variables.json ,但不要将process.env.SOME_ENVIRONMENT_VARIABLE放在JSON文件中。

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. 然后,从您提供的示例custom-environment-variables.json文件中,可以使用config.get('Customer.dbConfig.host') ,这将引用您在PROD_SERVER环境变量中设置的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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