我在Heroku上部署了Node js应用程序,尝试启动它时收到错误H12请求超时。不知道我的app.js文件出了什么问题。我正在使用node,express和ejs进行模板制作。也许我的GET方法有很多事情要做。任何有关H12错误的帮助将不胜感激。
我的代码:
const express = require("express");
const ejs = require("ejs");
const ping = require('ping');
const app = express();
app.set('view engine', 'ejs');
app.get("/", function(req, res){
var Data = [];
const hosts = [
'8.8.8.8',
'nibbler',
'10.204.2.14',
'nic-bqnbnntkwq.dynamic-m.com',
'co-omaha-wired-qqrrbzpgwq.dynamic-m.com',
'co-austin-cjjbbdqcwq.dynamic-m.com',
'fctx-wired-vtwpbhcgwq.dynamic-m.com',
'172.16.10.1',
'fc-greenwood-dzrrbnzqwq.dynamic-m.com',
'fcpa-3400-wired-nndkzwnrwq.dynamic-m.com',
'fcpa-chrome-gwmjjghjwq.dynamic-m.com',
'fcga-wired-bjkwtcjqwq.dynamic-m.com',
];
for(var i =0; i<hosts.length; i++){
ping.promise.probe(hosts[i]).then(function (resp) {
const destination = ['Azure DC','FC Nevada','Google DNS', 'Austin', 'Omaha',
'FC Greenwood', 'FC Georgia', 'FC Pennsylvania 3500', 'FC Pennsylvania 35400',
'FC Dallas', 'Nibbler DNS', 'NIC'];
if(resp.alive){
const input = {
"Destination": "",
"Host": resp.host,
"Status": "Alive",
"Avg": resp.avg
}
Data.push(input);
}
else {
const input = {
"Destination": "",
"Host": resp.host,
"Status": "Dead",
"Avg": resp.avg
}
Data.push(input);
}
if(Data.length == 12){
Data.sort(function(a, b) {
var A = a.Host.toUpperCase();
var B = b.Host.toUpperCase();
return (A < B) ? -1 : (A > B) ? 1 : 0;
});
for(var i = 0; i<destination.length; i++){
Data[i].Destination = destination[i];
}
res.render("home", {data: Data});
}
});
}
});
app.listen(process.env.PORT || 3000);
答案 0 :(得分:0)
H12表示您的应用需要30秒钟以上才能完成对请求的响应。 https://devcenter.heroku.com/articles/error-codes#h12-request-timeout
对所有这些主机执行ping操作可能很容易超过30秒。您在开发环境中计时了吗?
在heroku上,应在工人dyno上运行长时间运行的任务。在这种情况下,工作人员可以定期对这些主机执行ping操作,然后将结果写入某种数据存储区(redis或postgres)。该Web应用程序只需从数据存储中提取结果并呈现页面即可。
Heroku在https://devcenter.heroku.com/articles/node-redis-workers
上有一些很好的文档