【发布时间】:2022-04-24 05:08:44
【问题描述】:
我正在关注 the official instructions 将我的 strapi starter app 部署到 Heroku。该应用程序在本地运行良好。我在部署说明中唯一遗漏的是安装 PG 节点模块(它已经安装,因为我的本地应用程序使用 Postgresql)。
访问 Heroku 日志,我看到了:
error: Middleware "strapi::session": App keys are required.
Please set app.keys in config/server.js (ex: keys: ['myKeyA', 'myKeyB'])
也许这是一个重要的细节:我遵循了这个过程一次,一切正常。我能够部署到 Heroku。我又试了一次,还是不行。我在想 Heroku 可能在我重用应用名称时遇到了问题,但我尝试在 Heroku 中为应用命名不同的名称,但我仍然遇到同样的错误。
heroku 是否在错误的位置查找我的 server.js 文件?它应该在我的“./config/env/production”文件夹而不是我的“./config”文件夹中查找吗?
按照说明,这是我的 ./config/env/production/database.js
const parse = require('pg-connection-string').parse;
const config = parse(process.env.DATABASE_URL);
module.exports = ({ env }) => ({
connection: {
client: 'postgres',
connection: {
host: config.host,
port: config.port,
database: config.database,
user: config.user,
password: config.password,
ssl: {
rejectUnauthorized: false
},
},
debug: false,
},
});
这是我的 ./config/env/production/server.js
module.exports = ({ env }) => ({
url: env('MY_HEROKU_URL'),
});
这是我的 ./config/server.js
module.exports = ({ env }) => ({
host: env('HOST', '0.0.0.0'),
port: env.int('PORT', 1337),
app: {
keys: env.array('APP_KEYS'),
},
});
我的 package.json 很好衡量:
{
"dependencies": {
"@strapi/plugin-graphql": "^4.0.0",
"@strapi/plugin-i18n": "4.0.6",
"@strapi/plugin-users-permissions": "4.0.6",
"@strapi/strapi": "4.0.6",
"lodash.set": "^4.3.2",
"pg": "8.6.0",
"pg-connection-string": "^2.5.0"
},
"name": "backend",
"private": true,
"version": "0.1.0",
"description": "A Strapi application",
"scripts": {
"develop": "strapi develop",
"start": "strapi start",
"build": "strapi build",
"strapi": "strapi"
},
"devDependencies": {},
"author": {
"name": "A Strapi developer"
},
"strapi": {
"uuid": "f64b509e-2d95-4464-8d39-d6f0d1c7a31a",
"template": "@strapi/template-corporate@^1.0.0",
"starter": "@strapi/starter-next-corporate"
},
"engines": {
"node": ">=12.x.x <=16.x.x",
"npm": ">=6.0.0"
},
"license": "MIT"
}
我正在运行 Node v14.18.3 和 NPM v6.14.15
【问题讨论】:
-
Heroku 现在将为我运行该应用程序,但前提是我直接在 ./config/env/production/server.js 文件中列出密钥。任何人都知道为什么 Heroku 没有选择我在根 .env 文件中设置的全局变量吗?
-
原来我的 .env 在 .gitignore 中,所以 heroku 无法读取密钥。
-
我可以使用“heroku config:set”命令直接将密钥添加到heroku,这样我就可以将.env 保存在.gitignore 中