javascript - 节点 JS 应用程序的 Heroku 'H12' 请求超时错误
问题描述
我在 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);
解决方案
H12 意味着您的应用需要超过 30 秒才能完成对请求的响应。https://devcenter.heroku.com/articles/error-codes#h12-request-timeout
ping 所有这些主机可能需要 30 多秒。您是否在开发环境中对其进行了计时?
在 heroku 上,长时间运行的任务应该在工人测功机上运行。在这种情况下,工作人员可以定期 ping 这些主机并将结果写入某种数据存储(redis 或 postgres)。Web 应用程序将简单地从数据存储中提取结果并呈现页面。
Heroku 在这里有一些很好的文档https://devcenter.heroku.com/articles/node-redis-workers
推荐阅读
- c++ - 为 C++ 开发配置 Opensuse Leap 15.2
- laravel - Laravel 码头工人部署
- html - 当有圆角时,如何阻止主题背景颜色泄漏?
- python - 'urllib' 无限循环
- python - 带有验证令牌的 Python 发布请求
- python - 按数量分组获得前 3 名
- scala - Spark数据框替换scala中不同数据类型的空值
- python - 无法从 Docker 容器访问本地主机
- three.js - Three.js无法读取未定义的属性“制服”
- sql - /register 关系“small_small_hr_staffprofile”处的 ProgrammingError 不存在