快递超时问题,总是得到回复,强制阻塞操作时 [英] Express timeout issue, always getting back response,when forcing blocking operation

查看:62
本文介绍了快递超时问题,总是得到回复,强制阻塞操作时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试连接超时模块.

I'm trying out the connect-timeout module.

I've tried hitting a simple route from the browser

var timeout = require('connect-timeout');

app.use(timeout('1s'));
app.use(haltOnTimedout);

app.get('/timeout', function (req, res) { 
  for (var i = 0; i < 1111211111; i++) {}
  res.send('d') 
})
function haltOnTimedout(req, res, next){
  if (!req.timedout) next();
}

但我总是回到浏览器中(我认为超时会阻止它).有什么我没有得到的吗?

But I'm always getting back in the browser (I thought the timeout would prevent it). Anything I'm not getting here?

推荐答案

问题是你的 for 循环在一秒钟内执行.试试这个:

The problem is that your for loop executes in under a second. Try this:

app.get('/timeout', function (req, res) {
  setTimeout(function() {
    res.send('d');
  }, 5000);
});

在这个例子中,路由不会返回响应,直到 5 秒超时函数执行.但是,连接超时中间件将在此之前停止执行.

In this example, the route wont return a response until the 5 second timeout function executes. However, the connect-timeout middleware will halt execution before then.

这篇关于快递超时问题,总是得到回复,强制阻塞操作时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆