Koa: Node.js default timeout

Created on 4 Jul 2016  路  3Comments  路  Source: koajs/koa

I've been using Koa v2 to implement an API and recently ran into an issue with long lasting HTTP requests, where they would crash after 120 seconds. Curl (which I was using to test this API) would return the error curl: (52) Empty reply from server
I also noticed that Koa skipped the whole part where the middlewares would filter and manipulate the response upstream (probably because the HTTP connection was closed).

It turns out that node's HTTP server has a default timeout of 120 seconds.

I managed to disable this timeout in Koa by creating this simple middleware:

app.use(async (ctx, next) => {
    ctx.req.setTimeout(0);
    await next();
});

I'm not sure if it is within the scope of this project to disable this default behavior, but I thought of at least sharing the knowledge as someone else might find it useful.

Most helpful comment

We can just use server.setTimeout to change default timeout.
require('koa')().listen(port) will return server instance. :)
https://nodejs.org/api/http.html#http_server_settimeout_msecs_callback

All 3 comments

I'm not sure if it is within the scope of this project to disable this default behavior

I think we'll probably stick to the HTTP module defaults. Thanks for the tip though!

I agree with @PlasmaPower, this is a node issue, and chances are more people expect the timeout to exist than not. I'm closing this, but yes, thanks for the suggestion :)

We can just use server.setTimeout to change default timeout.
require('koa')().listen(port) will return server instance. :)
https://nodejs.org/api/http.html#http_server_settimeout_msecs_callback

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rowild picture rowild  路  4Comments

SteveCruise picture SteveCruise  路  5Comments

usernameisalreadytaken2014 picture usernameisalreadytaken2014  路  4Comments

coodoo picture coodoo  路  4Comments

tracker1 picture tracker1  路  3Comments