[Fixed] Nodejs Express response.download() Not Behaving as Expected


I’m using following code to track active download counts.

router.get('/download/:fileName', (req, res) => {
    let fileName = req.params.fileName;
    let baseFilePath='/home/filePath';


    return res.download(
        function (err) {
            if (err) {
                console.log(`ERROR ++ : ${err}`);
            } else {
                console.log('download completed');

When each time download started it increments download count by one, but after few seconds ‘download completed’ is printed to the log and download count get decremented while the downloading is in progress.

As of the nodejs express documentation the callback function is called after the download is finished or in an event of exception. Can anyone please help on this ?

I’m using nodejs 14.16.0 and express 4.17.1


Finally found the reason behind this scenario.
I was using a nginx server as a reversed proxy to the node application and nginx cache the content and close the connection to the node application before closing the connection(s) with the client.

So this is fixed using

proxy_request_buffering off;
proxy_max_temp_file_size 0;

inside the location / block in nginx server

Leave a Reply

(*) Required, Your email will not be published