handle multer error json

Solutions on MaxInterview for handle multer error json by the best coders in the world

showing results for - "handle multer error json"
David
18 May 2016
1const upload = multer({
2    storage: storage,
3    limits: {
4        fileSize: 1024 * 1024 * 5
5    },
6    fileFilter: (req, file, cb) => {
7        if (file.mimetype == "image/png" || file.mimetype == "image/jpg" || file.mimetype == "image/jpeg") {
8            cb(null, true);
9        } else {
10            return cb(new Error('Invalid mime type'));
11        }
12    }
13});
14
15const uploadSingleImage = upload.single('image');
16
17app.post('/upload', function (req, res) {
18
19    uploadSingleImage(req, res, function (err) {
20
21        if (err) {
22            return res.status(400).send({ message: err.message })
23        }
24
25        // Everything went fine.
26        const file = req.file;
27        res.status(200).send({
28            filename: file.filename,
29            mimetype: file.mimetype,
30            originalname: file.originalname,
31            size: file.size,
32            fieldname: file.fieldname
33        })
34    })
35})
36