Heroku Node js部署问题:Web进程在启动后60秒内未能绑定到$ PORT

时间:2018-07-21 16:29:35

标签: node.js heroku deployment mlab

我正在尝试通过在Heroku上部署来测试示例节点js应用程序。我正在使用mlab作为我的数据库云托管。

但是很不幸,我遇到了一些错误。这是来自Heorku的错误日志:

2018-07-21T16:43:00.394188+00:00 heroku[web.1]: State changed from crashed to starting
2018-07-21T16:43:03.124528+00:00 heroku[web.1]: Starting process with command `npm start`
2018-07-21T16:43:06.239097+00:00 app[web.1]: 
2018-07-21T16:43:06.239139+00:00 app[web.1]: > quizgiri@1.0.0 start /app
2018-07-21T16:43:06.239141+00:00 app[web.1]: > node app.js
2018-07-21T16:43:06.239142+00:00 app[web.1]: 
2018-07-21T16:43:08.063152+00:00 app[web.1]: listening on port 3000 ...... 
2018-07-21T16:44:03.476862+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2018-07-21T16:44:03.476977+00:00 heroku[web.1]: Stopping process with SIGKILL
2018-07-21T16:44:03.631909+00:00 heroku[web.1]: Process exited with status 137
2018-07-21T16:44:03.646925+00:00 heroku[web.1]: State changed from starting to crashed
2018-07-21T16:44:06.757320+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/tasks" host=quizgiri.herokuapp.com request_id=9379fd55-584e-4693-8d51-75e06c1cce91 fwd="103.25.120.134" dyno= connect= service= status=503 bytes= protocol=https
2018-07-21T16:44:12.243533+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=quizgiri.herokuapp.com request_id=6654c0c4-5c8b-4e52-9539-27d52a3b0c6f fwd="103.25.120.134" dyno= connect= service= status=503 bytes= protocol=https
2018-07-21T16:49:04.284636+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/tasks" host=quizgiri.herokuapp.com request_id=a1ed02bb-2fd7-491b-bbaf-81675c15a3c4 fwd="103.25.120.134" dyno= connect= service= status=503 bytes= protocol=https
2018-07-21T16:49:05.395033+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=quizgiri.herokuapp.com request_id=168d3a5c-8eb7-45fe-b16e-c1e3c4ef36a4 fwd="103.25.120.134" dyno= connect= service= status=503 bytes= protocol=https

我的节点js应用只有两个文件。

app.js:

const express = require("express");
var router = express.Router();
const app = express();
var mongojs = require('mongojs');

const port = process.env.port || 3000;

var db = mongojs("my mlab connection string", ['testcollection']);

//Get All Tasks
app.get('/tasks', function(req, res, next){
    console.log(db);
    db.testcollection.find(function(err, tasks){
        if(err){
            res.send(err);
        }
        res.json(tasks);
    });
});

app.listen(port, () => {
    console.log(`listening on port ${port} ...... `);
});

package.json:

{
  "name": "quizgiri",
  "version": "1.0.0",
  "description": "node server for quizgiri app",
  "main": "app.js",
  "scripts": {
    "start": "node app.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Anurag Bhattacharjee",
  "license": "ISC",
  "dependencies": {
    "express": "^4.16.3",
    "mongodb": "^3.1.0",
    "mongojs": "^2.6.0"
  }
}

1 个答案:

答案 0 :(得分:2)

发生此错误是因为您的Node.js应用程序配置不正确,无法通过$ PORT环境变量绑定到Heroku提供的端口上。

替换此行后尝试

const port = process.env.port || 3000;

使用

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