为什么我的heroku node.js应用会给出at =错误代码= H10 desc =“应用崩溃"?方法=获取路径="/"? [英] Why my heroku node.js app is giving at=error code=H10 desc="App crashed" method=GET path="/"?
本文介绍了为什么我的heroku node.js应用会给出at =错误代码= H10 desc =“应用崩溃"?方法=获取路径="/"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在Heroku上运行我的简单节点应用程序.
I am trying to run my simple node app on Heroku.
这是目录结构
├── app.js
├── assets
├── blog.html
├── index.html
├── node_modules
└── package.json
这是我的app.js
Here is my 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");
});
这是package.json
Here is the 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"
}
}
当我进入控制台时,它正确的打印应用程序是从xxxx端口开始的. 但是随后应用崩溃,并显示以下消息
When I go to the console it rightly prints app is starting at xxxx port. But then the app crashes with the following message
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
推荐答案
像这样设置端口
ES5:
var port = process.env.PORT || 8000;
和
server.listen(port, function() {
console.log("App is running on port " + port);
});
ES6:
const port = process.env.PORT || 8000;
和
server.listen(port, () => {
console.log("App is running on port " + port);
});
这允许Heroku在运行时设置端口.
This allows Heroku to set the port at run time.
这篇关于为什么我的heroku node.js应用会给出at =错误代码= H10 desc =“应用崩溃"?方法=获取路径="/"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文