Let's assume you have a server in koa and you have a POST route which takes ~ 3 minutes to return a request.
According to the various sources, you set-up your server with the timeout by setting up
let app = new Koa();
let server=app.listen(3000);
server.timeout=5*60*1000; // set to 5 minutes
On the client side, you set-up a timeout as well and you send the post request with the timeout
const request = await axios.post('https://somewebsite.herokuapp.com/testkeepalive', {}, {timeout:240000})
console.log('request is ', request)
Why when sending the above, on the server it still returns a timeout error and doesn't execute as intended?
Info about /testkeepalive
.post('/testkeepalive', async ctx => {
console.log('request received')
ctx.request.socket.setTimeout(5 * 60 * 1000)
ctx.request.socket.setKeepAlive(true)
await delay(3 * 60 * 1000) // we delay 3 mintues before returning the request
ctx.body = "nice"
ctx.status = 202
})
EDIT: Code above is correct. The only issue was that I was using Heroku which times out to 30seconds. Moving the server elsewhere (e.g AWS EC2 + deploy server with https ssl certificate) made it work.
If you still want to use heroku, you can only do it by implementing background jobs. https://devcenter.heroku.com/articles/background-jobs-queueing