Express timeout issue, always getting back response,when forcing blocking operation

给你一囗甜甜゛ 提交于 2019-12-11 05:23:35

问题


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?


回答1:


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);
});

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.



来源:https://stackoverflow.com/questions/37787255/express-timeout-issue-always-getting-back-response-when-forcing-blocking-operat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!