In couchdb, db.getAttachment
return a Stream
, so you can pipe that stream to http response object:
getAttachment().pipe(httpResponseObject)
Full code:
app.get("/download/:id/:file", function (req, res) {
var filename = req.params.file;
var docI= req.params.id;
if(userHasPrivileges()){
res.attachment(filename);
db.getAttachment(publicationID, filename, function(err){ }).pipe(res);
}
}
If your attachments are big data such as big images, mp3s or videos, you should support range download. Range in headers may allow browsers download/re-download the part they need.