为什么我的 heroku node.js 应用程序给出 at=error code=H10 desc="App crashed" method=GET path="/"?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38874760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:28:48  来源:igfitidea点击:

Why my heroku node.js app is giving at=error code=H10 desc="App crashed" method=GET path="/"?

node.jsheroku

提问by Saras Arya

I am trying to run my simple node app on Heroku.

我正在尝试在 Heroku 上运行我的简单节点应用程序。

Here is the directory structure

这是目录结构

├── app.js
├── assets
├── blog.html
├── index.html
├── node_modules
└── package.json

Here is my app.js

这是我的 app.js

let express = require('express'),
    path = require('path');
var app = express();
let server = require('http').Server(app);

app.use(express.static(path.join(__dirname)));

app.get('/', function(req, res, next){
    res.sendStatus(200);
});

app.get('/blog.html', function(req, res,next){
    res.sendFile(path.join(__dirname+"/blog.html"));
});

app.post('/contact', function(req, res, next){

});
server.listen('8000', function() {
    console.log("App is running on port 8000");
});

Here is the package.json

这是 package.json

{
  "name": "website",
  "version": "1.0.0",
  "engines" : {
    "node" : "6.3.1",
    "npm" : "3.10.3"
  },
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start" : "node app.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.14.0"
  }
}

When I go to the console it rightly prints app is starting at xxxx port. But then the app crashes with the following message

当我转到控制台时,它正确地打印了应用程序正在从 xxxx 端口启动。但随后应用程序崩溃并显示以下消息

2016-08-10T13:12:49.839138+00:00 app[web.1]: App is running on port xxxx
2016-08-10T13:13:34.944963+00:00 heroku[router]: at=error code=H20 desc="App boot timeout" method=GET path="/" host=saras-website.herokuapp.com request_id=28d8705a-d5a4-4aaa-bd8d-4c4c6101fbd4 fwd="106.51.20.181" dyno= connect= service= status=503 bytes=
2016-08-10T13:13:48.295315+00:00 heroku[web.1]: State changed from starting to crashed
2016-08-10T13:13:48.552740+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=saras-website.herokuapp.com request_id=b77e151f-7017-482d-b4ba-15d980534fd7 fwd="106.51.20.181" dyno= connect= service= status=503 bytes=
2016-08-10T13:13:50.163466+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=saras-website.herokuapp.com request_id=1e7b57e5-1056-4cb3-b41f-cd3f11794efe fwd="106.51.20.181" dyno= connect= service= status=503 bytes=

I don't know what am I doing wrong here... Help is appreciated

我不知道我在这里做错了什么......感谢帮助

回答by alexi2

Set port like this

像这样设置端口

ES5:

ES5:

var port = process.env.PORT || 8000;

And

server.listen(port, function() {
    console.log("App is running on port " + port);
});

ES6:

ES6:

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

And

server.listen(port, () => {
    console.log("App is running on port " + port);
});

This allows Heroku to set the port at run time.

这允许 Heroku 在运行时设置端口。