在本地 运行 应用程序时下载或设置 Heroku 应用程序配置变量

Download or set Heroku app config variables while running the app locally

我有一个从 GitHub 下载的应用程序,将其推送到 Heroku 后,它运行得非常好。但是,当我在本地使用 heroku local 命令 运行 时,它会抛出有关缺少配置变量的错误。

如何将这些配置文件下载到我的本地文件夹(我什至需要这样做)?我是否需要手动创建 .env 文件才能使其正常工作?

这是应用程序:

const assert = require('assert');
const Provider = require('oidc-provider');

assert(process.env.HEROKU_APP_NAME, 'process.env.HEROKU_APP_NAME missing');
assert(process.env.PORT, 'process.env.PORT missing');
assert(process.env.SECURE_KEY, 'process.env.SECURE_KEY missing, run `heroku addons:create securekey`');
assert.equal(process.env.SECURE_KEY.split(',').length, 2, 'process.env.SECURE_KEY format invalid');

// new Provider instance with no extra configuration, will run in default, just needs the issuer
// identifier, uses data from runtime-dyno-metadata heroku here
const oidc = new Provider(`https://${process.env.HEROKU_APP_NAME}.herokuapp.com`, {
  clients: [
    {
      client_id: 'foo',
      redirect_uris: ['https://jwt.io'], // using jwt.io as redirect_uri to show the ID Token contents
      response_types: ['id_token'],
      grant_types: ['implicit'],
      token_endpoint_auth_method: 'none',
    },
  ],
  cookies: {
    keys: process.env.SECURE_KEY.split(','),
  },
});

// Heroku has a proxy in front that terminates ssl, you should trust the proxy.
oidc.proxy = true;

// listen on the heroku generated port
oidc.listen(process.env.PORT);

这是我在 运行 "heroku local":

时得到的错误
3:44:57 AM web.1 |  assert.js:383
3:44:57 AM web.1 |      throw err;
3:44:57 AM web.1 |      ^
3:44:57 AM web.1 |  AssertionError [ERR_ASSERTION]: process.env.HEROKU_APP_NAME missing
3:44:57 AM web.1 |      at Object.<anonymous> (C:\Users\hmffa\Desktop\my-provider\src\index.js:4:1)
3:44:57 AM web.1 |      at Module._compile (internal/modules/cjs/loader.js:1063:30)
3:44:57 AM web.1 |      at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
3:44:57 AM web.1 |      at Module.load (internal/modules/cjs/loader.js:928:32)
3:44:57 AM web.1 |      at Function.Module._load (internal/modules/cjs/loader.js:769:14)
3:44:57 AM web.1 |      at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
3:44:57 AM web.1 |      at internal/main/run_main_module.js:17:47 {
3:44:57 AM web.1 |    generatedMessage: false,
3:44:57 AM web.1 |    code: 'ERR_ASSERTION',
3:44:57 AM web.1 |    actual: undefined,
3:44:57 AM web.1 |    expected: true,
3:44:57 AM web.1 |    operator: '=='
3:44:57 AM web.1 |  }
[DONE] Killing all processes with signal  SIGINT
3:44:57 AM web.1 Exited with exit code null

这些变量通常因环境而异。有些东西,例如您的数据库连接字符串和安全密钥,应该在本地和生产中有所不同。

然而,Heroku documents an easy way to copy a config var from Heroku to a local .env file:

Sometimes you may want to use the same config var in both local and Heroku environments. For each config var that you want to add to your .env file, use the following command:

heroku config:get CONFIG-VAR-NAME -s  >> .env

Do not commit the .env file to source control. It should only be used for local configuration. Update your .gitignore file to exclude the .env file.

请注意,.env 文件并不神奇。 Node.js 不会立即使用其中的值。

如果你use heroku local to run your app locally (as you appear to be) you should find that your .env file gets used. If you run it another way, you may need to do additional work, e.g. perhaps by adding dotenv.