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); });