how to validate before multer upload?

Viewed 25

I am creating userProfile in that i want to validate user details and if all data are in required format then only i want to upload the profile picture of user. for that i created the validator middleware and then upload.single but I am getting empty array on req.body. What could be solution so that I can validate first then upload the image code:

let uniqueSuffix;
const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, "public/profile_pic");
  },
  filename: function (req, file, cb) {
    uniqueSuffix = Date.now() + "-" + file.originalname.toLocaleLowerCase();
    cb(null, uniqueSuffix);
  },
});

const upload = multer({ storage });

const validator = (req, res, next) => {
  console.log(req.body);//logs empty array
  next();
};

router.post("/signup", validator, upload.single("profile"), (req, res) => {
  console.log(req.body);//i am getting whole body data here
});
1 Answers

you can use joi and validate req.body

Related