const app = require('express')();
const session = require('express-session');
const {
check,
validationResult
} = require('express-validator/check');
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: true,
cookie: { secure: false }
}))
app.get("/test", [
// username must be an email
check('username').not().isEmpty(),`//.withCustomMessage() based on the content of req.session`
// password must be at least 5 chars long
check('password').not().isEmpty()
],(req,res)=>{
console.log("req.session", req.session);
// Finds the validation errors in this request and wraps them in an object with handy functions
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(422).json({ errors: errors.array() });
}
//console.log("req.session",req.session);
});
app.get("/",(req,res,next)=>{
req.session.message = "beoulo ";
// console.log(req.session);
res.status(200).json({
"status" :"session set"
});
});
app.listen(3000,()=>{
console.log("Listening on port 3000!!!");
});
将 Check 直接作为中间件传递是使用它的唯一方法吗?我们是否仍然可以在单独的中间件函数中使用 req.checkbody(field,errormessage) 格式或等效的格式,因为必须从会话中获取错误消息
我想从 req.session 访问一个变量,并基于它生成一个自定义错误消息
以前的实现工作正常,因为它使用 req.checkBody()
有了新的变化,我应该怎么做才能处理这种情况。