简体   繁体   中英

Stream buffer to client in Express

I have request handler to send file from MongoDB (GridFS) to client like below, but it use data variable so content is in memory. I need to make this in streaming mode and send file in chunks to client. I can't regognize how to pipe buffer to response. Look at second code - it doesn't work, but show something what i need.

Maybe it is useful: Data in GridFS is Base64 encoded, but may be changed if streaming can be more efficient.

In-Memory version

router.get('/get/:id', function(req,res){
  getById(req.params.id, function(err, fileId){
    new GridStore(db, fileId, "r").open(function(err, gridStore) {
        res.set('Content-Type', gridStore.contentType);

        var stream = gridStore.stream(true);
        var data = '';
        stream.on("data", function(chunk) {
            data += chunk;
        });
        stream.on("end", function() {                   
            res.send(new Buffer(data, 'base64'));                   
        });
    });
  });
});

Streaming mode version

router.get('/get/:id', function(req,res){
  getById(req.params.id, function(err, fileId){
    new GridStore(db, fileId, "r").open(function(err, gridStore) {
        res.set('Content-Type', gridStore.contentType);

        var stream = gridStore.stream(true);
        stream.on("data", function(chunk) {
            new Buffer(chunk, 'base64').pipe(res);
        });
        stream.on("end", function() {                   
            res.end();
        });
    });
  });
});

Update

I think I'm close to resolve this. I found this works, but does't decode from Base64:

new GridStore(db, fileId, "r").open(function(err, gridStore) {
    res.set('Content-Type', gridStore.contentType);
    gridStore.stream(true).pipe(res);
});

I found a solution, but think that can be better. I use base64-stream module to decode Base64 stream. Solution below:

router.get('/get/:id', function(req,res){
    getById(req.params.id, function(err, fileId){
        new GridStore(db, fileId, "r").open(function(err, gridStore) {
            res.set('Content-Type', gridStore.contentType);
            gridStore.stream(true).pipe(base64.decode()).pipe(res);
        });
    });
}); 
exports.sendFile = function(db, res, fileId) {
  var grid = require('gridfs-stream');
  var gfs = grid(db, mongoose.mongo);
  var on_error = function(){
    res.status(404).end();
  };
  var readstream = gfs.createReadStream({
    filename: fileId,
    root: 'r'
  });
  readstream.on('error', function(err) {
    if (('\'' + err + '\'') === '\'Error:  does not exist\'') {
      return on_error && on_error(err);
    }
    throw err;
  });
  return readstream.pipe(res);
}
stream.on("data", function(chunk) {
    res.send(chunk.toString('utf8'));
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM