【问题标题】:Download or set Heroku app config variables while running the app locally在本地运行应用程序时下载或设置 Heroku 应用程序配置变量
【发布时间】:2021-08-11 17:53:18
【问题描述】:

我有一个从 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

【问题讨论】:

    标签: node.js heroku heroku-cli


    【解决方案1】:

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

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

    有时您可能希望在本地环境和 Heroku 环境中使用相同的配置变量。对于要添加到 .env 文件的每个配置变量,请使用以下命令:

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

    不要将.env 文件提交到源代码管理。它应该只用于本地配置。更新您的 .gitignore 文件以排除 .env 文件。

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

    如果您是use heroku local to run your app locally(看起来是这样),您应该会发现您的.env 文件已被使用。如果你以另一种方式运行它,你可能需要做额外的工作,例如也许通过添加dotenv

    【讨论】:

    • 感谢克里斯的回答。实际上,在使用您提到的命令将配置变量添加到“.env”文件之后 Heroku 没有正确读取输入。我发现它使用 UTF-16 LE 编码创建了 .env 文件。我将其更改为 UTF-8 并且有效。
    • 嗯,这很奇怪。我想知道这是否与您的外壳有关?还是.env 文件已经存在?无论如何,我很高兴你能成功。
    • 我使用的是 Windows PowerShell 而不是 Linux 操作系统。也许这导致了问题。不,我没有.env 文件
    猜你喜欢
    • 1970-01-01
    • 2020-02-20
    • 1970-01-01
    • 2016-04-09
    • 2020-02-16
    • 2020-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多