1

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

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

但我总是回到浏览器中(我认为超时会阻止它)。有什么我不明白的吗?

4

1 回答 1

0

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.

于 2016-06-13T11:04:44.877 回答