meteor-restivus
meteor-restivus copied to clipboard
Pipe stream to response object
Hi,
So I was writing a WebService endpoint that will be used to retrieve a file. I know that is should be done this way :
var server = http.createServer(function (req, res) {
var stream = fs.createReadStream(__dirname + '/data.txt');
stream.pipe(res);
});
So I read the documentation and I found this :
this.response
Node response object
Naturally I tried this code in my app :
if (Meteor.isServer) {
Api.addRoute('database', {
get: function() {
let fileStream = DatabasePackages.findOne().createReadStream();
fileStream.pipe(this.response);
fileStream.on('error', function(err) {
console.log('err:' + err);
});
fileStream.on('data', function(data) {
console.log('data:' + data);
});
this.response.writeHead(200, {
'Content-Type': 'application/octet-stream',
'Content-Disposition': 'attachment; filename=\'db.sqlite\''
});
this.done();
return {};
}
});
}
The data appears in my console but the file my browser downloads is empty... Do you have any idea?
@antoin-m did you ever find a solution to this? @kahmali any suggestions? I assumed it would be as simple as doing this:
fileStream.on('end', function() {
this.done();
});
But that gives me the Error: Must call this.done() after handling endpoint response manually
error.
@nickw No we dropped Meteor in favor of Laravel. You should take a look at IronRouter's restful routes.
Sorry for not having any good answer right now, and completely missing this for so long. I just answered a similar question (probably unsatisfactorily) here.
Maybe this workaround will help to deal with this.done() issue ... https://github.com/kahmali/meteor-restivus/issues/25#issuecomment-254930202