Express 유효성 검사

Namlulu·2021년 12월 22일
0

Node

목록 보기
3/5
// ...rest of the initial code omitted for simplicity.
const { body, validationResult } = require('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, res) => {
    // 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));
  },
);

=> 매번 유효성 검사 로직을 안 짜도 된다. express에서 사용 가능한 유효성 검사 라이브러리이다.

profile
Better then yesterday

0개의 댓글