为什么我的 heroku MERN stack 应用程序崩溃了?

Why is my heroku MERN stack app crashing?

我在 heroku 上部署了我的 mern stack 应用程序,但出现错误,我不知道是什么原因。 错误日志

 2020-04-22T15:25:46.000000+00:00 app[api]: Build succeeded
 2020-04-22T15:25:58.707869+00:00 app[web.1]: 
 2020-04-22T15:25:58.707896+00:00 app[web.1]: > contactkeeper@1.0.0 start /app
 2020-04-22T15:25:58.707897+00:00 app[web.1]: > node server.js
 2020-04-22T15:25:58.707897+00:00 app[web.1]: 
 2020-04-22T15:25:59.586133+00:00 app[web.1]: Server started on port 5000..
2020- 04-22T15:25:59.792525+00:00 app[web.1]: Database connected...
2020-04-22T15:26:56.331376+00:00 heroku[web.1]: State changed from starting to crashed
2020-04-22T15:26:56.334688+00:00 heroku[web.1]: State changed from crashed to starting
2020-04-22T15:27:07.526858+00:00 app[web.1]: 
2020-04-22T15:27:07.526882+00:00 app[web.1]: > contactkeeper@1.0.0 start /app
2020-04-22T15:27:07.526883+00:00 app[web.1]: > node server.js
2020-04-22T15:27:07.526883+00:00 app[web.1]: 
2020-04-22T15:27:08.176741+00:00 app[web.1]: Server started on port 5000..
2020-04-22T15:27:08.300897+00:00 app[web.1]: Database connected...
2020-04-22T15:28:05.725653+00:00 heroku[web.1]: State changed from starting to crashed
2020-04-22T15:28:08.166135+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=quiet-stream-92502.herokuapp.com request_id=e53971ab-65b3-4aa5-bf96-678ab2e6101b fwd="105.112.99.16" dyno= connect= service= status=503 bytes= protocol=https
2020-04-22T15:28:09.099701+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=quiet-stream-92502.herokuapp.com request_id=a0b04322-cb79-472b-8c93-19c55d3e0f1b fwd="105.112.99.16" dyno= connect= service= status=503 bytes= protocol=https

重复
Found Answer

另外,为了理解
来自 Heroku 的文档: Common Runtime 通过防火墙将所有 dynos 彼此隔离开来提供强大的隔离。唯一可以到达 dyno 的流量是从路由器转发到侦听 $PORT 环境变量中指定的端口号的 Web 进程的 Web 请求。 Worker 和一次性 dynos 无法接收入站请求。
[...]

dynos common-runtime-networking

这意味着您不能设置自定义端口。
您必须使用名为 "PORT"

的动态生成的环境变量

如果你仔细查看你的错误,你会注意到它在端口 5000 上显示 运行。你收到错误的原因是你没有使用 Heroku 分配给你的应用程序的端口号.

由于您使用的是快递,因此您的 app.listen 应该如下所示

const PORT = process.env.PORT || 5000;

app.listen(PORT, () => {
  console.log("server has started");
});

确保不要5000放在process.env.PORT之前。当您尝试部署到 heroku 时,这会导致错误。