TypeError:req.checkBody不是一个函数

我试图在注册系统中实现一些validation,但我得到的错误:

TypeError: req.checkBody is not a function 

从下面的代码:

 module.exports = function(app, express) { var express = require('express'); var api = express.Router(); // post users to database api.post('/signup', function(req, res) { var email = req.body.email; var password = req.body.password; var password2 = req.body.password2; var key = req.body.key; // Validation req.checkBody('email', 'Email is required.').notEmpty(); req.checkBody('email', 'Email is not valid').isEmail(); req.checkBody('password', 'Password is required').notEmpty(); req.checkBody('password2', 'Passwords do not match').equals(req.body.password); var errors = req.validationErrors(); if(errors) { res.render('register', { errors: errors }); } else { var user = new User({ email: email, password: password }); var token = createToken(user); } // save to database user.save(function(err) { if (err) { res.send(err); return; } res.json({ success: true, message: 'User has been created', token: token }); }); }); 

我已经检查过,它从前端获取信息,而且我在另一个应用程序中的代码工作几乎相同(其中未包含在module.exports = function(app,express){}

你需要使用下面的命令来安装express-validator

 npm install express-validator 

然后加

 var expressValidator = require('express-validator'); api.use(expressValidator()) 

之后立马

 var api = express.Router(); 

请参阅TypeError:有关更多详细信息, req.checkBody不是包含bodyparser和expressvalidator模块的函数