Reputation: 373
Hi Every One I'm Trying To Install express-validator in my app But I'm Facing TypeError: validator is not a function
When I Tried To Require The Moduleand Use It In app.use() Function
app.js
Code
var validator = require("express-validator");
---
app.use(express.json());
app.use(express.urlencoded({ extended: false })); app.use(cookieParser());
app.use(validator());
Upvotes: 0
Views: 669
Reputation: 12552
require("express-validator")
is not a middleware. The middlewares are:
check([field, message])
body([fields, message])
oneOf(validationChains[, message]) ..etc..
Basic example taken from doc:
// ...rest of the initial code omitted for simplicity.
const { check, validationResult } = require('express-validator');
app.post('/user', [
// username must be an email
check('username').isEmail(),
// password must be at least 5 chars long
check('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(422).json({ errors: errors.array() });
}
User.create({
username: req.body.username,
password: req.body.password
}).then(user => res.json(user));
});
Upvotes: 3