Created
July 23, 2013 11:56
-
-
Save veer66/6061807 to your computer and use it in GitHub Desktop.
Retrieve image from 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
var express = require('express'); | |
var app = express(); | |
var Db = require('mongodb').Db, | |
MongoClient = require('mongodb').MongoClient, | |
ObjectID = require('mongodb').ObjectID, | |
GridStore = require('mongodb').GridStore; | |
app.get("/myimage.png", function(req, res) { | |
MongoClient.connect("mongodb://localhost:27017/mydb", function(err, db_) { | |
var fileId = new ObjectID("51ebba1b2ca657f0d0000087"); | |
var file = new GridStore(db_, fileId, "r"); | |
file.open(function(err, file) { | |
res.set('Content-Type', file.contentType); | |
var stream = file.stream(); | |
stream.pipe(res); | |
}); | |
}); | |
}); | |
app.listen(nconf.get('port')); | |
console.log('Listening on port ' + nconf.get('port')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment