I'm writing code for image upload in NodeJS using multer
My code is
var upload = multer({
storage: storage,
limits: {
fileSize: 1024 * 1024 * 5
},
fileFilter: (req, file, cb) => {
if (file.mimetype == "image/png" || file.mimetype == "image/jpg" || file.mimetype == "image/jpeg") {
cb(null, true);
} else {
return cb(JSON.stringify({ "success": false, "message": "invalid mime type" }), false);
}
}
});
router.post('/upload', upload.single('image'), (req, res, next) => {
const io = req.app.get('io');
const product = new db.product({
name: req.body.name,
category: req.body.category,
image: req.protocol + "://" + req.hostname + ":" + req.socket.localPort + "/img/roundtshirt/" + req.file.filename
});
});
When I upload an invalid file from Postman, I got the following error such as my callback
<pre>{"success":false,"message":"invalid mime type"}</pre>
However, I want to convert that error to proper json format.
I've tried returning json but got an error.