问题
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