Buffer stream for client in Express

I have a request handler to send a file from MongoDB (GridFS) to the client, as shown below, but it uses the data variable to keep the contents in memory. I need to do this in streaming mode and send the file to pieces to the client. I cannot recognize as a pipe buffer for the response. Look at the second code - it does not work, but it will show something that I need.

Perhaps this is useful: the data in GridFS is encoded by Base64, but can be changed if streaming can be more efficient.

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 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 that. I found this to work, but not decrypted from Base64:

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

I found a solution, but I think it might be better. I am using the base64-stream module to decode a Base64 stream. The solution is 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); }); }); }); 
+1
source
 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); } 
+1
source
 stream.on("data", function(chunk) { res.send(chunk.toString('utf8')); }); 
0
source

All Articles