Strapi を Heroku にデプロイする際に発生する「ミドルウェア "strapi::session": アプリキーが必要です」というエラーを解決するにはどうすればよいですか? 質問する

Strapi を Heroku にデプロイする際に発生する「ミドルウェア

フォローしています公式の指示私の展開ストラピスターターアプリ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」フォルダーではなく、「./config/env/production」フォルダーを探す必要がありますか?

指示に従って、ここに私の./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を実行しています

ベストアンサー1

私はこれで解決しました./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

おすすめ記事