J7s*_*tin 24 heroku node.js strapi
我正在按照官方说明将我的Strapi 入门应用程序部署到 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
Tem*_*emo 15
我用这个解决了这个问题./config/env/production/server.js
module.exports = ({ env }) => ({
  url: env("MY_HEROKU_URL"),
  proxy: true,
  app: {
    keys: env.array("APP_KEYS", ["testKey1", "testKey2"]),
  },
});
testKey1,testKey2只是占位符,需要通过heroku中的CONFIG VAR替换为2个随机密钥
APP_KEYS=someSecret,anotherSecret
proxy: true也很重要。否则它会抛出一个Cannot send secure cookie over unencrypted connection
正如 @Temo 提到的,将环境变量添加到文件中并不是正确的解决方案。尽管它有效,但它带来了相当多的安全威胁。
您应该做的是将其添加APP_KEYS到 Heroku 上的环境变量中。您可以通过使用以下代码创建文件来生成新密钥:
// filename: generateCode.js
const crypto = require('crypto')
console.log(crypto.randomBytes(16).toString('base64'))
然后从控制台运行它:
node generateCode.js
它生成的代码类似于foP7OJcuRhCw1sTR6EfZPw==. 将其用作 Heroku 中的 APP_KEY。
.env只需在项目的根目录中创建文件,如下所示:
HOST=0.0.0.0
PORT=1337
APP_KEYS=jP8pb1lYsAhnmURarewxhA==,34xnLMYHY5jiU7ONTstTqQ==
| 归档时间: | 
 | 
| 查看次数: | 22073 次 | 
| 最近记录: |