1

我没有通过包含“express-validator”完成加载的应用程序,显然我定义、使用或调用的位置不正确,正如对此问题的评论中所建议的那样。

这是我的 app.js 的一部分,感谢所有建议:

var express = require('express')
    , expressValidator = require('express-validator')
    , routes = require('./routes')
    , http = require('http')
    , path = require('path');

...

var app = express();

app.set('port', process.env.PORT || port);
app.set('views', __dirname + '/views');
app.engine('.html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use(express.logger('dev'));
app.use(express.bodyParser());

// The problem
app.use(expressValidator);

app.use(express.methodOverride());
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.cookieParser());
app.use(express.session({ secret: 'keyboard test' }));    

app.use(function(req, res, next){
    var err = req.session.error
      , msg = req.session.success;
    delete req.session.error;
    delete req.session.success;

    res.locals.message = '';
    if (err) res.locals.message = '<p class="msg error">' + err + '</p>';
    if (msg) res.locals.message = '<p class="msg success">' + msg + '</p>';
    next();
});    

app.use(app.router);

...

app.post('/login', function(req, res){
    req.assert('email', 'Range email').len(6, 40);
    req.assert('password', 'Range password').len(6, 20);

    var errors =  req.onValidationError();

    if(errors.length == 0)
    {

...

4

1 回答 1

7

用法expressValidator是一个会返回中间件函数的函数,所以需要显式调用:

app.use(expressValidator());
于 2013-06-29T19:19:41.257 回答