0

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.

4

2 回答 2

0

PROD_SERVER 可以是您尝试连接的数据库服务器的名称,如果您需要从 Heroku 配置中读取它,则需要在 heroku 中设置配置。

假设您在 heroku 中设置了一个名为“DATABASE_URL”的环境变量,您可以在代码中以 process.env.DATABASE_URL 的形式访问它。

Heroku 应用程序的环境变量将在应用程序的设置选项卡中提供。在“配置变量”下

当您使用 process.env.SOME_ENVIRONMENT_VARIABLE 时,您的服务器会崩溃,因为该值未在配置中设置,您可以查看 Heroku 应用程序的控制台以获取更多详细信息。

于 2018-03-19T05:20:06.883 回答
0

我是node-config.

PROD_SERVER在文档中是环境变量名称的示例。

您可以放入SOME_ENVIRONMENT_VARIABLEcustom-environment-variables.json但不要放入process.env.SOME_ENVIRONMENT_VARIABLEJSON 文件。

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

于 2018-03-19T19:41:35.963 回答