Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pipe stream to response object #177

Open
antoin-m opened this issue Feb 3, 2016 · 5 comments
Open

Pipe stream to response object #177

antoin-m opened this issue Feb 3, 2016 · 5 comments

Comments

@antoin-m
Copy link

antoin-m commented Feb 3, 2016

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 antoin-m changed the title Pipe file to response object Pipe stream to response object Feb 3, 2016
@boomfly
Copy link

boomfly commented Jul 18, 2016

@nickw
Copy link

nickw commented Aug 3, 2016

@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.

@antoin-m
Copy link
Author

antoin-m commented Aug 4, 2016

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

@kahmali
Copy link
Owner

kahmali commented Aug 4, 2016

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.

@MartinBucko
Copy link

Maybe this workaround will help to deal with this.done() issue ... #25 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants