Skip to content

Instantly share code, notes, and snippets.

@jakemmarsh
Created Sep 8, 2013
Embed
What would you like to do?
Express endpoint to retrieve file from MongoDB GridFS
exports.get = function(req, res) {
var db = new mongo.Db('downloadr', new mongo.Server("127.0.0.1", 27017, {}), {safe: false, strict: false});
db.open(function (err) {
if (err) return handleError(err);
var gfs = Grid(db, mongo);
gfs
// create a read stream from gfs...
.createReadStream({ _id: req.param('fileId') })
.on('error', function() {
res.send(500, 'failed to retrieve file.');
})
// and pipe it to Express' response
.pipe(res);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment