Express: What is the default timeout of a req in express?

Created on 7 Jun 2017  路  11Comments  路  Source: expressjs/express

Uploading a blob on azure storage and at times the request timeout before the blob successfully uploads. How can I solve this? Thanks in adv

question

Most helpful comment

@flickz @vitoravale I've found two ways to make it work.

You may set the timeout either globally for entire server:

var server = app.listen();
server.setTimeout(500000);

or just for specific route:

app.post('/xxx', function (req, res) {
   req.setTimeout(500000);
});

IMHO the second sounds much proper :)

All 11 comments

Hi @flickz by default, Express sits on top of the built-in Node.js HTTP server.

You can find the documentation on the timeout in the Node.js documentation: https://nodejs.org/dist/latest-v6.x/docs/api/http.html#http_server_settimeout_msecs_callback

The default for 6.x is 2 minutes. You can use server.setTimeout(msecs) to set a different default timeout on the server.

I hope this helps!

@dougwilson Thanks

@flickz did you solve the problem?

@vitoravale It's almost a year I can't really remember what we did as the project didn't work out. But I think it has to do with the azure storage module I used not giving the progress status of the blob while uploading, I filed the issue there.

@flickz @vitoravale I've found two ways to make it work.

You may set the timeout either globally for entire server:

var server = app.listen();
server.setTimeout(500000);

or just for specific route:

app.post('/xxx', function (req, res) {
   req.setTimeout(500000);
});

IMHO the second sounds much proper :)

is there a way to know that the server closed because of timeout?

@ktrzeciaknubisa
for some reason, the second way is not working for me
req.setTimeout(500000);

Figured it out, it was a typo 馃槗
It should be res, instead of req

res.setTimeout(3000, function () {
    console.log('here')
  });

Here's my solution:

const apiTimeout = 10 * 1000;
app.use((req, res, next) => {
    // Set the timeout for all HTTP requests
    req.setTimeout(apiTimeout, () => {
        let err = new Error('Request Timeout');
        err.status = 408;
        next(err);
    });
    // Set the server response timeout for all HTTP requests
    res.setTimeout(apiTimeout, () => {
        let err = new Error('Service Unavailable');
        err.status = 503;
        next(err);
    });
    next();
});

Here's my solution:

const apiTimeout = 10 * 1000;
app.use((req, res, next) => {
    // Set the timeout for all HTTP requests
    req.setTimeout(apiTimeout, () => {
        let err = new Error('Request Timeout');
        err.status = 408;
        next(err);
    });
    // Set the server response timeout for all HTTP requests
    res.setTimeout(apiTimeout, () => {
        let err = new Error('Service Unavailable');
        err.status = 503;
        next(err);
    });
});

There should be a next() call in the end of the middleware.

Thanks! I putted next() in auth.

Was this page helpful?
0 / 5 - 0 ratings