I have an application built on MEAN stack and hosted on heroku servers. I use mLab for managing the database side of the application. The data which is uploaded to mLab can be retrieved successfully but the data which is in the form of pdf saved to mLab using gridfs cannot be retrieved. The app throws Application error
and in the logs I can see the heroku error code H13 which says Connection closed without response. I'm guessing it is because the pdf should be downloaded on visiting a url
, and when the server redirects the user to that url
, it takes some time to process the chunks and pipe the stream to the response
, and the application doesn't wait for the reponse
and returns without it and hence, the error.
CODE
from the angular js controller the user is redirected to /getFile/:url
using $window.location='/getFile/'+url
index.js
app.get('/getFile/:url',function(req,res){
var dbs = mongoose.connection.db;
var mongoDriver = mongoose.mongo;
var gfs = new Gridfs(dbs,mongoDriver);
var p = new Promise(function(resolve, reject) {
db.find({url:req.params.url},function(err,data){
gfs.exist({_id:data[0].file},function(err,found){
if(found){
var readstream = gfs.createReadStream({
_id: data[0].file
});
res.set({
'Content-Disposition':'attachment;filename=clip.pdf'
})
resolve(readstream)
db.remove({url:req.params.url},function(err,result){
gfs.remove({_id:data[0].file})
})
} else {
reject()
}
})
})
});
p.then(function(val){
val.pipe(res)
}).catch(function(){
res.redirect('/')
})
})
Can anyone help in overcoming this?
via Ritik Saxena
No comments:
Post a Comment