简体   繁体   中英

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';

    incrementDownloadCount(fileName);

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM