Node JS REST API image upload, how do i only upload jpg files and not png?

619 Views Asked by At

I am using express-fileupload as my middleware and POSTMAN as my API, how do I only upload jpg images and not others file extensions?

1

There are 1 best solutions below

0
On BEST ANSWER

You can check the mimetype and throw an error if it isn't .jpg

For example:

app.post('/upload', function(req, res) {
  if (req.files.image.mimetype !== "jpg") {
    throw new Error("Only supports jpg file format");
  }
});