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