Firstly you have to declare multer function and also you can add any properties to the function. I have mentioned some of the properties as well. here is for more info link
//uploading images and validations
const upload = multer({
limits: {
fileSize: 1000000 //maximum size of an image
},
fileFilter(req, file, cb) {
// checking if file extension does not match with jpg,png,jpeg
if (!file.originalname.match(/\.(jpg|png|jpeg)$/)) {
return cb(new Error('Please upload a image.')); // if it is then throw an error
}
cb(undefined, true);
}
});
and then you have to declare the route with sharp, basically sharp is
The typical use case for this high-speed Node.js module is to convert
large images in common formats to smaller, web-friendly JPEG, PNG and
WebP images of varying dimensions.
here is the link for more info.
//create photo
router.post('/',upload.array('pic', 10),async (req, res) => {
try {
const promises = req.files.map((file) => {
return sharp(file.buffer).resize({ width: 1024,
height: 1024}).png().toBuffer();
});
const buffers = await Promise.all(promises);
const photos = buffers.map((buffer) => {
return new Photo({ pic: buffer});
});
await Photo.insertMany(photos);
res.redirect('/');
} catch (e) {
res.status(400).redirect('/');
}
},
(error, req, res, next) => {
res.status(400).redirect('/');
}
);