我正在尝试使用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

最佳答案

更改

app.use(expressValidator());




app.use(validator());

关于javascript - ReferenceError:未定义expressValidator Express,ejs,Nodejs,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48219919/

10-12 23:04