Skip to content

Instantly share code, notes, and snippets.

@veer66
Created July 23, 2013 11:56
Show Gist options
  • Save veer66/6061807 to your computer and use it in GitHub Desktop.
Save veer66/6061807 to your computer and use it in GitHub Desktop.
Retrieve image from GridFS
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