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

前端 未结 2 952
孤城傲影
孤城傲影 2021-01-28 06:37

Is there any supported way how to deploy a Strapi backend and a ReactJS frontend into a single Heroku app?

I have successfully deployed the Strapi backend, but am complet

2条回答
  •  甜味超标
    2021-01-28 06:55

    In your strapi app create the following files in the following directories:

    ./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"
        ]
      }
    }
    

提交回复
热议问题