错误 R10(启动超时)-> Web 进程未能在启动后 60 秒内绑定到 $PORT - HEROKU 错误

Sri*_*i S 8 javascript hosting heroku node.js express

我将 Node.js WebApp 部署到 heroku 但收到此错误

2021-06-01T09:19:42.615419+00:00 heroku[web.1]: State changed from crashed to starting
2021-06-01T09:19:47.259832+00:00 heroku[web.1]: Starting process with command `node app.js`
2021-06-01T09:19:51.146182+00:00 app[web.1]: Server is running on port 3001.
2021-06-01T09:20:47.916699+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to 
bind to $PORT within 60 seconds of launch
2021-06-01T09:20:47.989032+00:00 heroku[web.1]: Stopping process with SIGKILL
2021-06-01T09:20:48.124402+00:00 heroku[web.1]: Process exited with status 137
2021-06-01T09:20:48.196055+00:00 heroku[web.1]: State changed from starting to crashed
2021-06-01T09:24:45.072782+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET 
path="/" host=positate.herokuapp.com request_id=7e9ec2b1-5685-4c3f-9c29-6c03268b7c82 
fwd="157.51.56.186" dyno= connect= service= status=503 bytes= protocol=https
2021-06-01T09:24:46.540443+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET 
path="/favicon.ico" host=positate.herokuapp.com request_id=4ab44a6a-4795-4a4d-b32d-28d796845774 
fwd="157.51.56.186" dyno= connect= service= status=503 bytes= protocol=https
Run Code Online (Sandbox Code Playgroud)

我在这里附上我的 app.js

2021-06-01T09:19:42.615419+00:00 heroku[web.1]: State changed from crashed to starting
2021-06-01T09:19:47.259832+00:00 heroku[web.1]: Starting process with command `node app.js`
2021-06-01T09:19:51.146182+00:00 app[web.1]: Server is running on port 3001.
2021-06-01T09:20:47.916699+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to 
bind to $PORT within 60 seconds of launch
2021-06-01T09:20:47.989032+00:00 heroku[web.1]: Stopping process with SIGKILL
2021-06-01T09:20:48.124402+00:00 heroku[web.1]: Process exited with status 137
2021-06-01T09:20:48.196055+00:00 heroku[web.1]: State changed from starting to crashed
2021-06-01T09:24:45.072782+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET 
path="/" host=positate.herokuapp.com request_id=7e9ec2b1-5685-4c3f-9c29-6c03268b7c82 
fwd="157.51.56.186" dyno= connect= service= status=503 bytes= protocol=https
2021-06-01T09:24:46.540443+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET 
path="/favicon.ico" host=positate.herokuapp.com request_id=4ab44a6a-4795-4a4d-b32d-28d796845774 
fwd="157.51.56.186" dyno= connect= service= status=503 bytes= protocol=https
Run Code Online (Sandbox Code Playgroud)
这是我的 Procfile

web: node app.js
Run Code Online (Sandbox Code Playgroud)

这是我的 package.json

{
  "name": "positate",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "engines": {
    "node": "14.15.4",
    "npm": "6.14.10"
  },
  "scripts": {
    "start": "node app.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "connect-mongo": "^4.4.1",
    "dotenv": "^10.0.0",
    "ejs": "^3.1.6",
    "express": "^4.17.1",
    "express-session": "^1.17.2",
    "mongoose": "^5.12.11",
    "mongoose-findorcreate": "^3.0.0",
    "mongoose-timestamp": "^0.6.0",
    "passport": "^0.4.1",
    "passport-google-oauth20": "^2.0.0",
    "passport-local": "^1.0.0",
    "passport-local-mongoose": "^6.1.0"
  }
}
Run Code Online (Sandbox Code Playgroud)

我尝试了多种解决方案来解决此错误,但无法解决。过去三天我一直在努力解决这个问题。请帮我解决这个错误,非常感谢您。

Bep*_*e C 10

问题是你定义端口的方式,它总是3001在 Heroku 上不可能运行,你需要绑定$PORTenv 变量。
更改您的代码以检查第一个 fiprocess.env.PORT的定义(它将在 Heroku 上,但在您的本地开发环境中它将默认为 3001)

app.listen(process.env.PORT || 3001, '0.0.0.0', () => {
  console.log("Server is running.");
});
Run Code Online (Sandbox Code Playgroud)

请参阅Heroku 上的 NodeJS