0
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() を使用していたため、以前の実装は正常に機能しました

新しい変更により、このシナリオを処理するにはどうすればよいですか。

4

1 に答える 1