Skip to content

Instantly share code, notes, and snippets.

@jakemmarsh
Created September 8, 2013 00:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakemmarsh/6480714 to your computer and use it in GitHub Desktop.
Save jakemmarsh/6480714 to your computer and use it in GitHub Desktop.
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