如何解决“中间件“strapi::session”:应用程序密钥是必需的”错误在我的Strapi部署到Heroku?

jexiocij  于 2022-11-24  发布在  其他
关注(0)|答案(6)|浏览(126)

我正在按照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"
}

我正在运行节点v14.18.3和NPM v6.14.15

drkbr07n

drkbr07n1#

我在./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

6fe3ivhb

6fe3ivhb2#

在Heroku上,对于特定的应用程序,导航到设置-〉配置变量,并在那里添加您的环境变量。

0x6upsns

0x6upsns3#

只需在项目根目录下创建.env文件,如下所示:

HOST=0.0.0.0
PORT=1337
APP_KEYS=jP8pb1lYsAhnmURarewxhA==,34xnLMYHY5jiU7ONTstTqQ==
ncecgwcz

ncecgwcz4#

像@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。

yyhrrdl8

yyhrrdl85#

所以你只需要在Heroku settings-〉config vars中创建一个名为APP_KEYS的变量,这个变量的值可以从你的.env文件中得到,你应该有一个带值的APP_KEYS变量。

nxowjjhe

nxowjjhe6#

从git ignore中删除.env,然后再次按下。

相关问题