So obviously there is a knowledge gap in my basics and hence can't figure this out-
const validate = validations => {
return async (req, res, next) => {
console.log('check 3')
await Promise.all(validations.map(validation => validation.run(req)));
const errors = validationResult(req);
if (errors.isEmpty()) {
return next();
}
res.status(400).json({ errors: errors.array() });
};
};
This is being called by
app.post('/api/create-user', validate([
body('email').isEmail(),
body('password').isLength({ min: 6 })
]), async (req, res, next) => {
// request is guaranteed to not have any validation errors.
res.send("succ")
});
the body() function inside validate in app.post is from express-validator
Now, how is app.post calling that middleware function const validate? I don't understand how the arguments are passing.
validations is the argument list there which should receive
validate([
body('email').isEmail(),
body('password').isLength({ min: 6 })
])
right?
But how is another brace opening saying return async (req, res, next) => {
the first 2 lines are where my confusion is
const validate = validations => {
return async (req, res, next) => {