Stream files in node/express to client

You can stream directly to the response object (it is a Stream).

A basic file stream would look something like this.

function(req, res, next) {
  if(req.url==="somethingorAnother") {
    res.setHeader("content-type", "some/type");
    fs.createReadStream("./toSomeFile").pipe(res);
  } else {
    next(); // not our concern, pass along to next middleware function
  }
}

This will take care of binding to data and end events.

Leave a Comment