1

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?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
SuperUberDuper
  • 9,242
  • 9
  • 39
  • 72

1 Answers1

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.

MrWillihog
  • 2,586
  • 19
  • 17