Node.js应用程序在Heroku上不断崩溃,导致错误启动超时

时间:2017-10-23 06:18:01

标签: javascript node.js heroku web-applications server

我正在开发一个示例node.js应用程序,它在

本地运行正常
node index.js

但是,当我将它推送到Heroku实例时,它会因以下错误而崩溃:

  

2017-10-23T06:08:07.000000 + 00:00 app [api]:构建成功
  2017-10-23T06:08:16.591817 + 00:00 heroku [web.1]:用命令node --debug=5858 index.js开始进程
  2017-10-23T06:08:17.873171 + 00:00 app [web.1]:监听[::]:5858的调试器   2017-10-23T06:08:18.051769 + 00:00 app [web.1]:服务器监听端口8080
  2017-10-23T06:09:16.966905 + 00:00 heroku [web.1]:错误R10(启动超时) - > Web进程在启动后60秒内无法绑定到$ PORT   2017-10-23T06:09:16.966905 + 00:00 heroku [web.1]:使用SIGKILL停止流程
  2017-10-23T06:09:17.062360 + 00:00 heroku [web.1]:进程退出状态137
  2017-10-23T06:09:17.107893 + 00:00 heroku [web.1]:状态从开始变为崩溃   2017-10-23T06:09:20.068453 + 00:00 heroku [router]:at = error code = H10 desc =“App crashed”method = GET path =“/ contact”host = obscure-meadow-84857.herokuapp.com request_id = 69c587a7-ba7f-49d3-8057-4b56338b2d01 fwd =“49.35.12.63”dyno = connect = service = status = 503 bytes = protocol = https
  2017-10-23T06:09:20.137463 + 00:00 heroku [router]:at = error code = H10 desc =“App crashed”method = GET path =“/”host = obscure-meadow-84857.herokuapp.com request_id = 44f48e7a-94aa-4c10-9578-e8f50f8aeec5 fwd =“49.35.12.63”dyno = connect = service = status = 503 bytes = protocol = https

我的package.json文件设置如下:

{
  "name": "testapp",
  "version": "1.0.0",
  "description": "A little test application",
  "main": "basicRouting.js",
  "dependencies": {
    "ejs": "^2.5.7",
    "express": "^4.16.2"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js"
  },
  "keywords": [
    "tutorial"
  ],
  "author": "Debaditya Dey",
  "license": "ISC"
}

有人可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:3)

2017-10-23T06:09:16.966905+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch

请为您的应用使用process.env.PORT,而不是使用自己的应用。

答案 1 :(得分:1)

在index.js文件中,像这样定义你的端口

const port = process.env.PORT || 8080;
app.listen(port, () => {
    console.log("Server listening on port " + port);
});