Issue
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
Solution
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