【问题标题】:how do you start an express server with react on heroku?你如何启动一个对heroku做出反应的快速服务器?
【发布时间】:2019-02-12 19:08:33
【问题描述】:

我已经设法让我的应用程序在 heroku 上运行,我可以点击 url 并成功加载。

问题在于这只是 React 前端(本地我在端口 3000 上启动)。然后,我还使用 express 节点服务器作为我的 api 端点的后端,然后使用 firebase 作为我的数据。在本地运行时,我一直在 3007 上启动它。但是,我刚刚部署并且它正在运行前端,但没有进行任何 API 调用(我认为它还没有启动服务器)

如何将 2 链接起来或告诉 heroku 也运行其他服务器命令?

【问题讨论】:

    标签: javascript node.js reactjs heroku


    【解决方案1】:

    假设你有一个文件夹结构

    -main folder
    --client folder
    --server stuff
    

    尝试写入服务器的 package.json

      "scripts": {
        "heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"
      }
    

    然后将项目从服务器文件夹级别部署到heroku

    这将在您部署整个项目期间构建您的客户端

    @编辑

    还加

    
    if (process.env.NODE_ENV === "production") {
      app.use(express.static("client/build"));
      const path = require("path");
      app.get("*", (req, res) => {
        res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
      });
    }
    

    到服务器的 index.js 文件

    【讨论】:

    • 好的,我的 src/ 文件夹应该放在哪里?我尝试将所有内容都放在客户端文件夹中,但它坏了
    • 好的,我现在已经在 heroku 上为我的路线提供服务,但它不会加载前端??
    猜你喜欢
    • 2017-08-29
    • 2015-06-11
    • 2020-08-06
    • 1970-01-01
    • 1970-01-01
    • 2021-08-05
    • 2019-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多