1

我很好奇参数是如何传递给验证器中间件的,摘录自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(...);
  }
});
4

1 回答 1

2
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
于 2018-04-19T07:39:18.180 回答