我正在尝试将快速验证器用于我的注册表单。
我已经安装了:
npm install express-validator --save
然后将其添加到我的server.js文件中:
var validator = require('express-validator');
var app = express();
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost/cvit', {useNewUrlParser: true});
require('./config/passport');
app.use(express.static('public'));
app.engine('ejs', engine);
app.set('view engine', 'ejs');
app.use(cookieParser());
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());
app.use(validator());
app.use(session({
secret: 'This is my test key',
resave: false,
saveUninitialized: false,
store: new MongoStore({mongooseConnection: mongoose.connection})
}));
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
运行应用程序时,出现以下错误:
app.use(validator());
^
TypeError: validator is not a function
我不确定为什么会这样,因为我确实安装了它,但是好像它没有被识别。
任何提示都将受到高度赞赏。
编辑
我正在使用此验证功能
function validate(req, res, next){
req.checkBody('firstname', 'Firstname is Required').notEmpty();
req.checkBody('lastname', 'Lastname is Required').notEmpty();
req.checkBody('email', 'Email is Required').notEmpty();
req.checkBody('email', 'Email is Invalid').isEmail();
req.checkBody('password', 'Password is Required').notEmpty();
req.checkBody('password', 'Password Must Not Be Less Than 5').isLength({min:5});
req.check("password", "Password Must Contain at least 1 Number.").matches(/^(?=.*\d)(?=.*[a-z])[0-9a-z]{5,}$/, "i");
var errors = req.validationErrors();
if(errors){
var messages = [];
errors.forEach((error) => {
messages.push(error.msg);
});
req.flash('error', messages);
res.redirect('/signup');
}else{
return next();
}
}