Performance: express-validator setup
This affects server response time and user experience by validating input before processing requests.
Jump into concepts and practice - no test required
import { body, validationResult } from 'express-validator'; app.post('/submit', [ body('email').isEmail(), body('password').isLength({ min: 6 }) ], (req, res) => { const errors = validationResult(req); if (!errors.isEmpty()) { return res.status(400).json({ errors: errors.array() }); } res.send('Success'); });
app.post('/submit', (req, res) => { if (!req.body.email || !req.body.password) { return res.status(400).send('Missing fields'); } // manual validation logic here // proceed with processing res.send('Success'); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual validation in route handler | N/A (server-side) | N/A | N/A | [X] Bad |
| express-validator middleware usage | N/A (server-side) | N/A | N/A | [OK] Good |
express-validator in an Express app?body validator from express-validator?body, so use named import syntax.import { body } from 'express-validator'; which is correct for ES modules.email field is missing in the request body?
import { body, validationResult } from 'express-validator';
app.post('/signup', [
body('email').isEmail(),
body('password').isLength({ min: 6 })
], (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
res.send('Signup successful');
});isEmail() check, so validationResult(req) will contain errors.import { body, validationResult } from 'express-validator';
app.post('/login', (req, res) => {
body('username').notEmpty();
body('password').isLength({ min: 8 });
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(422).json({ errors: errors.array() });
}
res.send('Login successful');
});body('username').notEmpty() must be middleware before the route handler, not called inside it.app.post.email, password, and age. The rules are: email must be valid, password at least 8 characters, and age must be an integer between 18 and 99. Which express-validator setup correctly applies these rules and handles errors?!errors.isEmpty() to detect errors and respond with status 400. app.post('/register', [
body('email').isEmail(),
body('password').isLength({ min: 8 }),
body('age').isInt({ min: 18, max: 99 })
], (req, res) => {
res.send('Registration complete');
}); skips error checking. app.post('/register', [
body('email').isEmail(),
body('password').isLength({ min: 8 }),
body('age').isInt({ min: 18, max: 99 })
], (req, res) => {
const errors = validationResult(req);
if (errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
res.send('Registration complete');
}); reverses the condition incorrectly. app.post('/register', (req, res) => {
body('email').isEmail();
body('password').isLength({ min: 8 });
body('age').isInt({ min: 18, max: 99 });
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
res.send('Registration complete');
}); calls validators inside handler, which is wrong.