How to validate an email in Node.js and Express

1 Answer

0 votes
const express = require('express');
const { check, validationResult } = require('express-validator');

const router = express.Router();

router.post(
  '/',
  [
    check('email', 'Please include a valid email').isEmail()
  ],
  (req, res) => {
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
      return res.status(400).json({ errors: errors.array() });
    }
    res.send('User details okay');
  }
);


module.exports = router;

// Postman 

/*
{
	"email": "yourname@email.com"
}
*/

/*

run:

User details okay

*/

 



answered May 10, 2020 by avibootz
edited May 11, 2020 by avibootz
...