0

I have written a web service using node & express that runs automation on request and responds with the finalized report.

I have used jquery ajax (tried fetch API as well) in the client side to start the automation in the server and on completion, reports are compressed as zip and sent to the client.

Issue:

When the server responds within 2 min, the zip is downloaded successfully but when the server takes more than 2 min to respond, the http request fails with "ERR_EMPTY_RESPONSE" though there is no issue in the server side.
I learnt that by default there is no timeout for the HTTP requests but I am not sure why am I facing this issue.

I would like to know the reason behind this behavior.

Snippet of ajax request made in the client side:

                var promise = $.ajax({
                    url: 'http://localhost:8001/test',
                    method:'POST',
                    xhrFields: {
                        responseType: 'blob'
                    },
                    success: function(res){
                        downloadFromBlob(res);
                    },
                    error: function(xhr, statusText, err){
                        alert(statusText);
                    },
                    data: options,
                });

Snippet from webservice (I have just tried to reproduce the issue here. The automation takes nearly 15 min to complete):

  router.get('/test/', runAutomation);
  async function runAutomation(req, res, next){
        res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
        res.header('Access-Control-Allow-Origin', '*');
        res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
        const timeout = 120000;
        setTimeout(()=> {
            const path = 'reports.zip';
            res.download(path);
        }, timeout);
    },  
vbrin27
  • 723
  • 1
  • 6
  • 25

2 Answers2

1

This is caused not by the backend, but your browser - I have consistently seen the 2 minute timeout across all browsers at some point or another. You may want your backend to return a 202 Accepted immediately, and ping it for updates rather than trying to wait for it to complete in one shot.

josh.trow
  • 4,861
  • 20
  • 31
  • See https://stackoverflow.com/a/54187045/446747 also – josh.trow Sep 14 '20 at 15:37
  • But I have seen websites where the request takes like 10 min to respond but still, it doesn't fail and it completes without any error. How could that be? @josh.trow – vbrin27 Sep 15 '20 at 13:46
  • It may take 10 minutes to send the full response, but it could be sending back little bits until then perhaps, keeping the browser waiting and restarting that 2 minute timeout? – josh.trow Sep 16 '20 at 00:07
  • No @josh.trow . I found that the issue is with the node server. Setting server.setTimeout(0) solved the problem. Thank you! You can refer here - https://nodejs.org/api/http.html#http_server_settimeout_msecs_callback. By default. the node server timeout is 2 min. – vbrin27 Sep 16 '20 at 03:18
0

In express, if we check /bin/www file content, by default it sets the timeout to 120000 ms as below

 server.setTimeout(120000);

We need to update it. Ref: https://nodejs.org/api/http.html#http_server_settimeout_msecs_callback

vbrin27
  • 723
  • 1
  • 6
  • 25