Skip to content

Instantly share code, notes, and snippets.

Created Sep 8, 2013
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("", 27017, {}), {safe: false, strict: false}); (err) {
if (err) return handleError(err);
var gfs = Grid(db, mongo);
// 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment