MulterError: File too large : How to Handle this error?

  Kiến thức lập trình

i am building a register functionality and uploading a profile image and sending it to backend. There multer handles it. The file size limit is set to 100kb.
Everything works fine.
But when i upload file larger than 100 kb ,
this error comes :

MulterError: File too large

But what i want is if this error comes , i simply want to send a response directly as :

res.status(400).json({error : "File size too large"})

i have handled this res already on frontend.

How to send this above response if multer gives me the file too large error?

my multer middleware code :

import multer from "multer";

const storage = multer.diskStorage({
    filename: function (req, file, callback) {
        callback(null, file.originalname);
    }
});

const upload = multer(
    { storage: storage ,
      limits: { fileSize: 1024 * 100 }
    }
);

export default upload

my route code :

router.post('/signup' , upload.single('image') , signup)

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT