簡體   English   中英

如何將 Strapi 后端和 ReactJS 前端部署到單個 Heroku 應用程序

[英]How to deploy Strapi backend and ReactJS frontend to a single Heroku app

是否有任何受支持的方式如何將 Strapi 后端和 ReactJS 前端部署到單個 Heroku 應用程序中?

我已經成功部署了 Strapi 后端,但我完全無法理解或找到有關如何部署前端的教程。

在您的 Strapi 應用程序中,在以下目錄中創建以下文件:

./middlewares/serve-react/defaults.json

{
    "serve-react": {
        "enabled": true
    }
}

./middlewares/serve-react/index.js

'use strict';

/**
 * Module dependencies
 */

// Node.js core.
const fs = require('fs');
const path = require('path');
const koaStatic = require('koa-static');

/**
 * Serve react hook
 */

module.exports = strapi => {

  return {
    /**
     * Initialize the hook
     */

    async initialize() {
      const {maxAge, path: publicPath} = strapi.config.middleware.settings.public;
      const staticDir = path.resolve(strapi.dir, publicPath || strapi.config.paths.static);

  strapi.router.get(
    '/*',
    async (ctx, next) => {
      const parse = path.parse(ctx.url);
      ctx.url = path.join(parse.dir, parse.base);

      await next();
    },
    koaStatic(staticDir, {
      maxage: maxAge,
      defer: false, // do not allow other middleware to serve content before this one
    })
  );

  // if no resource is matched by koa-static, just default to serve index file
  // useful for SPA routes
  strapi.router.get('*', ctx => {
    ctx.type = 'html';
    ctx.body = fs.createReadStream(path.join(staticDir + '/index.html'));
  });
},
  };
};

./config/middleware.json

{
  "timeout": 100,
  "load": {
    "before": [
      "responseTime",
      "logger",
      "cors",
      "responses",
      "gzip"
    ],
    "order": [
      "Define the middlewares' load order by putting their name in this array is the right order"
    ],
    "after": [
      "parser",
      "router",
      "serve-react"
    ]
  }
}

如果有人想知道 Muhammad Ahmad 的解決方案是否有效 - 是的,它有效。

最初,我專門使用 MERN 堆棧並使用兩個文件夾 - 一個用於客戶端,另一個用於服務器。 只有 package.json 在根。 在 Heroku 上一切正常,所以我沒想到很難將后端切換到 Strapi ......

然后我決定嘗試將服務器端(Express)替換為Strapi。 之后,將整個案例部署到 Heroku 並在單個 Heroku 應用程序上運行時出現問題。

所以,項目結構是:

/root 
      - /client (React UI)
      - /server (Strapi)
      - package.json

為了使整個事情協同工作,有必要在內部進行一點修改

./middlewares/serve-react/index.js:

 const staticDir = path.resolve(strapi.dir, clientBuildPath || strapi.config.paths.static);

其中clientBuildPath../client/build

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM