【问题标题】:Express React app works locally but gives error on heroku deployExpress React 应用程序在本地工作,但在 heroku 部署时出错
【发布时间】:2019-06-18 19:58:12
【问题描述】:

我确定这已解决,但我无法让它为我工作。

在本地我的仓库使用它作为我的

/index.js

const express = require("express");
const keys = require("./config/keys");
const path = require("path");

const app = express();

app.use(express.static(__dirname));
app.use(express.static(path.join(__dirname, "public")));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());

// uses fuzeRoutes
require("./routes/routes")(app);

app.get("*", function(req, res) {
  res.sendFile(__dirname, "public", "index.html");
});

app.listen(process.env.PORT || 5000, function() {
  console.log(
    "Express server listening on port %d in %s mode",
    this.address().port,
    app.settings.env
  );
});

package.json

  "scripts": {
    "client-install": "npm install --prefix client",
    "start": "node index.js",
    "server": "nodemon index.js",
    "client": "npm run start --prefix client",
    "dev": "concurrently \"npm run server\" \"npm run client\"",
    "heroku-postbuild": "cd client && npm install && npm run build"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "axios": "^0.18.0",
    "concurrently": "^4.1.0",
    "express": "^4.16.4",
    "http-proxy-middleware": "^0.19.1",
    "nodemon": "^1.18.9"
  }

现在这在本地工作没有问题,但在 heroku deploy 我得到一个错误 at=error code=H13 desc="Connection closed without response" method=GET path="/"

查看其他问题,我看到它是由于"SSL termination occurs at Heroku's load balancers; they send your app plain (non-SSL) traffic, so your app should create a non-HTTPS server." 查看其他问题,我要么不理解解决方案,要么找不到正确的线程。

任何帮助将不胜感激

更新:

我能够解决 H13 错误的问题。

问题是:

app.use(express.static(path.join(__dirname, "public")));
//
//
//
app.get("*", function(req, res) {
  res.sendFile(__dirname, "public", "index.html");
});

应该是:

   app.use(express.static(path.join(__dirname, "client/public")));
    //
    //
    //
    app.get("*", function(req, res) {
      res.sendFile(__dirname,"client", "public", "index.html");
    });

现在的问题是,在部署时,我收到带有 at=info method=GET path="/" 的 200 状态代码,但页面返回空白。该页面的 HTML 显示它在构建路径中有一个 <div id="root">,但该页面不加载反应组件。

【问题讨论】:

  • 你有Procfile吗?
  • @zsgomori 我没有这个项目没有

标签: javascript node.js reactjs heroku


【解决方案1】:

我已将以下快速配置部署到 Heroku 到 create-react-app 应用程序:

app.use(express.static(path.join(__dirname, './client/public')))

app.get('*', function(_, res) {
  res.sendFile(path.join(__dirname, './client/public/index.html'), function(err) {
    if (err) {
      res.status(500).send(err)
    }
  })
})

你可以看到完整的code here

【讨论】:

  • 谢谢!这解决了他更改功能的问题。
猜你喜欢
  • 2015-07-24
  • 2020-07-17
  • 2021-04-17
  • 1970-01-01
  • 2014-08-25
  • 1970-01-01
  • 2020-12-08
  • 2022-06-29
  • 1970-01-01
相关资源
最近更新 更多