【问题标题】:This site can’t be reached when deployed to Heroku部署到 Heroku 时无法访问此站点
【发布时间】:2020-01-13 20:26:47
【问题描述】:

我有一个简单的 MERN 应用程序在本地开发并运行。但是,在部署到 Heroku 后,我有“无法访问站点”。

这是我的 Heroku 日志:

2019-09-12T00:33:00.665358+00:00 heroku[web.1]: Starting process with command `npm start`
2019-09-12T00:33:03.909328+00:00 app[web.1]:
2019-09-12T00:33:03.909347+00:00 app[web.1]: > myStreet@1.0.0 start /app
2019-09-12T00:33:03.909349+00:00 app[web.1]: > node server.js
2019-09-12T00:33:03.909351+00:00 app[web.1]:
2019-09-12T00:33:05.050233+00:00 heroku[web.1]: State changed from starting to up
2019-09-12T00:33:04.994283+00:00 app[web.1]: LISTENING ON PORT 3001
2019-09-12T00:33:05.082318+00:00 app[web.1]: (node:23) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
2019-09-12T00:33:05.229315+00:00 app[web.1]: connected to the database
2019-09-12T00:33:12.960721+00:00 app[web.1]: GET / 404 4.000 ms - 139
2019-09-12T00:33:12.960048+00:00 heroku[router]: at=info method=GET path="/" host=dry-hamlet-33823.herokuapp.com request_id=d8a5cd47-50fa-428a-92d0-97ccba40db05 fwd="65.95.137.53" dyno=web.1 connect=2ms service=14ms status=404 bytes=415 protocol=https

出了什么问题,我该如何解决?我使用 process.env.PORT 来设置端口。

============= 感谢大家的帮助,我通过观看https://www.youtube.com/watch?v=e1LaekAnVIMo解决了这个问题

【问题讨论】:

  • 你能发布你的服务器JS页面吗?或者,如果你有一个带有代码的 git repo。

标签: node.js reactjs heroku deployment


【解决方案1】:
LISTENING ON PORT 3001

这几乎肯定是一个错误。

您无法在 Heroku 上选择您的端口:它是 sets an environment variable PORT,您必须在启动服务器时使用它。虽然 Heroku 为您提供 3001 端口是可能的,但这似乎不太可能。

sample Node.js application 提供了一个示例,说明如何将 PORT 环境变量与 Express 一起使用。

【讨论】:

  • 我确实在代码中使用了 process.env.PORT。但是,我无法让 REACT 应用程序在 Heroku 上运行。 Git 仓库:github.com/mail2mz/myStreet
  • @mikezenon,那么为什么它说“正在侦听端口 3001”,而 3001 是您的默认值?您确定您正在运行您链接到的相同代码吗? 具体来说,“我无法让 React 应用程序正常工作”是什么意思?请参阅How to Ask
  • 是的,我将本地 git 推送到了我发送的链接和 Heroku。这是一个简单的 MERN 堆栈示例。它有 React 作为前端,Node.js 作为后端。 3001端口是后端正在监听的端口。
  • “端口 3001 是后端正在侦听的端口”——同样,您无法决定这一点。 Heroku 为您提供一个 端口,您可以使用。你不能使用多个,也不能选择你使用的端口。
【解决方案2】:

Heroku 将 PORT 设置为环境变量。所以相应地更改代码。

const port = process.env.PORT || 3001;

app.listen(port, () => {
    console.log('LISTENING ON PORT ' + port);
});

【讨论】:

    猜你喜欢
    • 2021-02-21
    • 1970-01-01
    • 2020-01-05
    • 2016-10-09
    • 1970-01-01
    • 1970-01-01
    • 2021-04-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多