我正在尝试使用Express和node制作验证表单。我对这一切都很陌生,我在我的控制台中收到了这个错误
ReferenceError:未定义expressValidator
index.js代码
var express = require('express'),
bodyParser = require('body-parser'),
validator = require('express-validator'),
routes = require('./routes'),
path = require('path'),
ejs = require('ejs'),
app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(expressValidator());
//post
app.post('/signup', routes.registerUser);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.set('view options', {layout: false});
//get
app.get('/', routes.index);
app.get('/signup', routes.signup);
app.get('/login', routes.login);
app.listen(3000,function(){
console.log("Server running at Port 3000");
});
在路线文件夹中,我只有一个js文件 index.js
exports.index = function(req, res){
res.render('index');
};
exports.signup = function(req, res){
res.render('signup');
};
exports.login = function(req, res){
res.render('index');
};
exports.registerUser = function(req, res){
req.check('uname', 'Username is required').notEmpty();
req.check('fname', 'First name is required').notEmpty();
req.check('lname', 'Last name is required').notEmpty();
req.check('email', 'Email is required').notEmpty();
req.check('mob', 'Mobile Number is required').notEmpty();
req.check('add', 'Address is required').notEmpty();
req.check('city', 'City is required').notEmpty();
req.check('url', 'URL is required').notEmpty();
req.check('pcode', 'PIN code is required').notEmpty();
req.check('password', 'Password is required').notEmpty();
var errors = req.validationErrors();
if(errors){
console.log("There may be errors");
res.redirect('signup');
}else{
var uname = req.body.uname;
var user = {
'fname' : req.body.uname,
'lname' : req.body.lname,
'email' : req.body.email,
'mob' : req.body.mob,
'add' : req.body.add,
'url' : req.body.url,
'pcode' : req.body.pcode,
'password' : req.body.password
};
}
};
当我运行应用程序时,我每次都得到这个
app.use(expressValidator()); ^ ReferenceError:未定义expressValidator
答案 0 :(得分:2)
更改
app.use(expressValidator());
到
app.use(validator());