我刚开始使用 hapi.js,在尝试延迟路由响应时遇到了问题。
我使用以下代码成功延迟了对 /query
路由的响应:
server.route([{
method: 'GET',
path: '/query',
config: {
handler: function(request, reply) {
var response = reply().hold();
db = request.server.plugins['hapi-mongodb'].db;
someFxn(callbackFxn, request, response);
}
}
}]);
var someFxn(cb, req, res){
var raw = {};
//... do lots of stuff that can take a long time
cb(req, res, raw);
}
var callbackFxn = function(request, response, data){
response.source = data;
response.send();
}
我遇到的问题是,有时 someFxn()
需要很长时间才能执行,这似乎可能导致超时并再次发出请求。如何更改请求的超时值,使其不再触发请求?
我非常感谢任何人可以给我的任何建议/线索。
最佳答案
您可能会遇到 2 分钟的默认 Node.js 套接字超时。您可以使用路由配置选项 config.timeout.socket
覆盖它:
timeout
- define timeouts for processing durations:...
socket
- by default, node sockets automatically timeout after 2 minutes. Use this option to override this behavior. Defaults toundefined
which leaves the node default unchanged. Set tofalse
to disable socket timeouts.
像这样设置以完全禁用路由超时:
server.route([{
method: 'GET',
path: '/query',
config: {
timeout: {
socket: false
},
handler: function(request, reply) {
...
}
}
}]);
关于javascript - 使用 hapi.js reply().hold() 时如何防止脚本重新加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36632081/