0

I am rather new to Node and am attempting to learn streaming; please correct me if my understanding is flawed.

Using fs.createReadStream and fs.createWriteStream together with .pipe method will effectively stream any kind of data. Also res.end method utilizes streaming by default.

So could we use fs.createReadStream together with res.end to create the same streaming effect? How would this look?

Under what circumstances would you normally use res.end?

Thank you

james murphy
  • 1,505
  • 5
  • 31
  • 57

1 Answers1

0

You can use pipe like:

readStream.pipe(res);

To stream some readable stream to the response.

See this answer for a working example of using it.

Basically it's something like:

var s = fs.createReadStream(file);
s.on('open', function () {
    s.pipe(res);
});

plus some error handling and MIME types support - see this for full code:

where you can find it used in three examples using three node modules:

  1. express
  2. connect
  3. http
Community
  • 1
  • 1
rsp
  • 107,747
  • 29
  • 201
  • 177