首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将Strapi后端和ReactJS前端部署到单个Heroku应用程序

如何将Strapi后端和ReactJS前端部署到单个Heroku应用程序
EN

Stack Overflow用户
提问于 2020-07-27 23:13:37
回答 2查看 1.2K关注 0票数 1

是否有任何受支持的方法可以将Strapi后端和ReactJS前端部署到单个Heroku应用程序中?

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

EN

回答 2

Stack Overflow用户

发布于 2020-08-27 21:49:52

在您的strapi应用程序中,在以下目录中创建以下文件:

./middlewares/serve-react/defaults.json

代码语言:javascript
复制
{
    "serve-react": {
        "enabled": true
    }
}

./middlewares/serve-react/index.js

代码语言:javascript
复制
'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/midleware.json

代码语言:javascript
复制
{
  "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"
    ]
  }
}
票数 1
EN

Stack Overflow用户

发布于 2020-11-10 08:17:45

如果有人想知道穆罕默德·艾哈迈德的解决方案是否有效--是的,它有效。

最初,我专门使用MERN堆栈,并使用了两个文件夹-一个用于客户端,另一个用于服务器。只有package.json在根目录下。在Heroku上一切都运行得很好,所以我没想到会发现很难将后端切换到Strapi……

然后,我决定尝试将服务器端(Express)替换为Strapi。在那之后,将整个案例部署到Heroku并在单个Heroku应用程序上运行它就会出现问题。

因此,项目结构是:

代码语言:javascript
复制
/root 
      - /client (React UI)
      - /server (Strapi)
      - package.json

为了使整个工作在一起,有必要修改内部的一些小变化。

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

代码语言:javascript
复制
 const staticDir = path.resolve(strapi.dir, clientBuildPath || strapi.config.paths.static);

其中,clientBuildPath../client/build.

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63118276

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档