Search code examples
reactjsapiexpresshttptimeout

Advice when working with long running post requests from a React app to NodeJS Backend


I have an application that runs into problems when 100+ items are being send to my nodejs backend for processing. The entire request can take up to 3 minutes due to external api call limits per minute.

I've tried both axios and superagent but both hit a timeout at 1-2 minutes and the frontend will error saying net::ERR_EMPTY_RESPONSE with axios and Error: Timeout exceeded at Request.push.RequestBase from superagent - but my backend will continue to process jobs and succeed.

In the express backend I've opened up the timeout to be 10 minutes following the advice here Nodejs and express server closes connection after 2 minutes.

I'm asking for advice as my only next thought would be to break up the results on my frontend and send many, smaller requests, to get the job done.

Thanks in advance for any help or advice.


Solution

  • On axios you can set own timeout timer. Jus Initialize enter point:

    const api = axios.create({
        baseURL: apiURL,
        timeout: 10 * 60 * 1000, // whatever time you want
    });
    

    and just use it like:

    api.get()
    api.post()
    ...