Created
September 8, 2013 00:24
-
-
Save jakemmarsh/6480714 to your computer and use it in GitHub Desktop.
Express endpoint to retrieve file from MongoDB GridFS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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