nodeJS prevent timeout on res.download nodeJS prevent timeout on res.download express express

nodeJS prevent timeout on res.download


A few small things first of all, i think it's supposed to be res.writeHead() instead of res.writeHeader(). Secondly once you send the headers you can not send them again. You are getting the error because res.download() uses res.sendFile() which re-sends the headers. You can't use res.download() or res.sendFile() or res.send() because all of these three methods implicitly set the headers and send as part of the response. You'll need to use a combination of res.writeHead() ,res.write() and res.end(). I am just GET so i can test it from the browser, you can use post obviously.

router.get('/test', function (req,res,next) { res.writeHead(200, {'Content-Type': 'application/json',         'Content-Disposition': 'attachment',         'filename':'package.json' }); fs.readFile("package.json",function (err,data) {     if (err) {         throw err;     }else{         setTimeout(function () {             res.write(data);             res.end()         },200000);     } }); function keepBusy(){     if(!res.finished){         res.writeProcessing();         setTimeout(keepBusy,1000)     } } setTimeout(keepBusy,1000);});

This works fine for me. res.writeProcessing() sends the 102 status code. It is recommended for requests that last greater than 20 second but this blog post says that

102 has been left out more recent revisions of HTTP specifications

but it works fine for me in most browsers. If you have some control over the client as well there can be a much better and reliable solution.