DEV Community

Ankan Saha
Ankan Saha

Posted on

Get image data from mongodb using mongoose and send to client side using nodejs and javascript

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var imageSchema = new Schema({
name: String,
data: Buffer,
contentType: String
});

var Image = mongoose.model('Image', imageSchema);

function getImage(name) {
Image.findOne({ name: name }, function (err, image) {
if (err) throw err;

res.setHeader('Content-Type', image.contentType);
res.send(image.data);
});
}

Top comments (0)