0

I'm trying to understand how to handle nodejs timeout on a get call.

I have a page which requires a long execution time before being rendered.

I would like to set a timeout, and if we hit the timeout, I would like to abort the request, and redirect the user to a timeout page.

I've done this in my route.js file :

app.get('/exec', isLoggedIn, function(req, res) {

var defaultTimeout = 180000;
  res.connection.setTimeout(defaultTimeout, function(t) {
     console.log("TIMED OUT !");

     res.render('exec-timeout.ejs', {
        user : req.user
     });
  });
});

But it doesn't work... It hits the timeout, it displays the "TIMED OUT" message in the console, but it doesn't redirect me to the exec-timeout page, it just keeps trying to run the execution and hits the timeout again...

How can I abort the request on timeout, and render to the page ?

Thanks a lot.

DanielKhan
  • 1,190
  • 1
  • 9
  • 18
TehesFR
  • 140
  • 2
  • 15
  • 1
    possible duplicate of [Express.js HTTP request timeout](http://stackoverflow.com/questions/7222467/express-js-http-request-timeout) – Rodrigo Medeiros Jun 08 '15 at 11:40
  • 1
    From my perspective what you are doing is odd because you are trying to set the timeout within the callback that gets called for the route you defined. I also do not yet understand why you should get the message on the console and the subsequent render call should not work. To say the least if you want to set a timeout on the connection you need to need to set it within a middleware and not within the callback of the request of which you want to handle the timeout. Look here for more information: http://stackoverflow.com/questions/21708208/express-js-response-timeout – DanielKhan Jun 08 '15 at 13:28
  • @DanielKhan thanks I used this ! – TehesFR Jun 08 '15 at 16:23

0 Answers0