gpt4 book ai didi

node.js - 如何使用 Node 和 Passport 验证非空字段?

转载 作者:太空宇宙 更新时间:2023-11-04 00:22:52 25 4
gpt4 key购买 nike

我需要验证登录的非空字段(发布请求),但收到此错误:

TypeError: req.checkBody is not a function
at D:\IDoo projects\ExpressGenerado\config\routes.js:76:9

这是我的路线文件:

  app.post('/login',function(req,res){
req.checkBody('email', 'Email is required').notEmpty();
req.checkBody('password', 'Password is required').notEmpty();
var errors = req.validationErrors();

if (errors) {

res.render('signup',{message: req.flash('loginMessage')});

}
else{
passport.authenticate('local-login', {
successRedirect : '/profile', // redirect to the secure profile section
failureRedirect : '/login', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
})(req,res);
}
});

我的入口点应用程序文件,在添加 app.use(expressValidator) 后,我的网络挂起并且无法验证,它什么也不做:

var express = require('express');
var expressValidator = require ('express-validator');
var app = express();
var multer = require('multer')
var constants = require('constants');
var constant = require('./config/constants');

var port = process.env.PORT || 8042;
var passport = require('passport');
var flash = require('connect-flash');
var path = require('path');

var morgan = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');

var dateFormat = require('dateformat');
var now = new Date();
app.use(expressValidator);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));


require('./config/passport')(passport); // pass passport for configuration


app.use(morgan('dev')); // log every request to the console
app.use(cookieParser()); // read cookies (needed for auth)

app.use(express.static(path.join(__dirname, 'public')));
app.set('views', path.join(__dirname, 'app/views'));
app.set('view engine', 'ejs');

app.use(session({ secret: 'zomaareenstukjetekstDatjenietzomaarbedenkt' }));
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session


require('./config/routes.js')(app, passport); // load our routes and pass in our app and fully configured passport

app.listen(port);
console.log('The magic happens on port ' + port);

最佳答案

你正在这样做:

app.use(expressValidator);

但是,expressValidator是一个返回中间件函数的函数,因此您需要像这样使用它:

app.use(expressValidator());

关于node.js - 如何使用 Node 和 Passport 验证非空字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43939817/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com