1// ...rest of the initial code omitted for simplicity.
2const { body, validationResult } = require('express-validator');
3
4app.post('/user', [
5 // username must be an email
6 body('username').isEmail(),
7 // password must be at least 5 chars long
8 body('password').isLength({ min: 5 })
9], (req, res) => {
10 // Finds the validation errors in this request and wraps them in an object with handy functions
11 const errors = validationResult(req);
12 if (!errors.isEmpty()) {
13 return res.status(422).json({ errors: errors.array() });
14 }
15
16 User.create({
17 username: req.body.username,
18 password: req.body.password
19 }).then(user => res.json(user));
20});
21