install below libraries
var express = require(‘express’);
var fs = require(‘fs’);
var mongoose = require(‘mongoose’);
var Schema = mongoose.Schema;
var multer = require('multer');
connect ur mongo db :
mongoose.connect(‘url_here’);
Define database Schema
var Item = new ItemSchema({
img: {
data: Buffer,
contentType: String
}
}
);
var Item = mongoose.model('Clothes',ItemSchema);
using the middleware Multer to upload the photo on the server side.
app.use(multer({ dest: ‘./uploads/’,
rename: function (fieldname, filename) {
return filename;
},
}));
post req to our db
app.post(‘/api/photo’,function(req,res){
var newItem = new Item();
newItem.img.data = fs.readFileSync(req.files.userPhoto.path)
newItem.img.contentType = ‘image/png’;
newItem.save();
});