Answers for "typescript express validator request body"

0

typescript express validator request body

// ...rest of the initial code omitted for simplicity.
import { body, validationResult } from 'express-validator';

app.post(  
  '/user',  
  // username must be an email  
  body('username').isEmail(),  
  // password must be at least 5 chars long  
  body('password').isLength({ min: 5 }),  
  (req: express.Request, res: express.Response) => {    
    // Finds the validation errors in this request and wraps them in an object with handy functions    
    const errors = validationResult(req);    
    if (!errors.isEmpty()) {      
      return res.status(400).json({ errors: errors.array() });    
    }    
    
    User.create({      
      username: req.body.username,      
      password: req.body.password,    
    }).then(user => res.json(user));  
  },
);
Posted by: Guest on September-28-2021

Code answers related to "typescript express validator request body"

Code answers related to "TypeScript"

Browse Popular Code Answers by Language