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.
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
Most helpful comment
We can just use
server.setTimeoutto change default timeout.require('koa')().listen(port)will return server instance. :)https://nodejs.org/api/http.html#http_server_settimeout_msecs_callback