我很好奇参数如何传递给验证器中间件,摘录取自express-validator,例如 programming_language 传递给check()函数。
const { check, oneOf, validationResult } = require('express-validator/check');
app.post('/start-freelancing', oneOf([
check('programming_language').isIn(['javascript', 'java', 'php']),
check('design_tools').isIn(['photoshop', 'gimp'])
]), (req, res, next) => {
try {
validationResult(req).throw();
// yay! we're good to start selling our skilled services :)))
res.json(...);
} catch (err) {
// Oh noes. This user doesn't have enough skills for this...
res.status(422).json(...);
}
});
答案 0 :(得分:1)
check('programming_language')
'programming_language'是一个请求参数。无需单独传递任何内容。它来自
req.body.programming_language
req.cookies.programming_language
req.headers.programming_language
req.params.programming_language
req.query.programming_language
'design_tools'
也是如此req.body.design_tools
req.cookies.design_tools
req.headers.design_tools
req.params.design_tools
req.query.design_tools