Heroku 节点 js 部署问题:Web 进程在启动后 60 秒内无法绑定到 $PORT

Heroku node js deployment issue: Web process failed to bind to $PORT within 60 seconds of launch

我正在尝试通过在 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"
  }
}

发生此错误是因为您的 Node.js 应用未正确配置为绑定到 Heroku 通过 $PORT 环境变量提供的端口以外的端口。

替换此行后再试

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

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