meteor-restivus icon indicating copy to clipboard operation
meteor-restivus copied to clipboard

Pipe stream to response object

Open antoin-m opened this issue 9 years ago • 5 comments

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 avatar Feb 03 '16 00:02 antoin-m

boomfly avatar Jul 18 '16 11:07 boomfly

@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 avatar Aug 03 '16 20:08 nickw

@nickw No we dropped Meteor in favor of Laravel. You should take a look at IronRouter's restful routes.

antoin-m avatar Aug 04 '16 09:08 antoin-m

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.

kahmali avatar Aug 04 '16 18:08 kahmali

Maybe this workaround will help to deal with this.done() issue ... https://github.com/kahmali/meteor-restivus/issues/25#issuecomment-254930202

MartinBucko avatar Oct 19 '16 20:10 MartinBucko