从中间件访问swagger路径参数

时间:2019-01-17 14:00:15

标签: node.js express swagger swagger-tools

如何从Node.js中检索一个不可靠的路径参数?

昂首阔步的定义:

  /objects/{id}:
    x-swagger-router-controller: object.controller
    get:
      summary: Get object
      operationId: getObject
      consumes:
        - application/json
      parameters:
        - in: path
          name: id
          type: string
          required: true

我尝试过的事情:

req.swagger.params['id'].value
req.query.id

我需要任何依赖吗?我当前的依赖项是

  "dependencies": {
    "body-parser": "^1.18.3",
    "express": "^4.12.3",
    "morgan": "^1.9.1",
    "swagger-express-mw": "^0.7.0",
    "swagger-tools": "^0.10.4",
    "swagger-ui": "^3.20.5",
    "web3": "^1.0.0-beta.36"
  }

1 个答案:

答案 0 :(得分:0)

更改此项:req.swagger.params['id'].value-> req.swagger.params.id.value

如果您使用Express框架和Express路由器,则可以按其名称访问请求参数。例如,可以像这样访问/objects/{id}:路径参数:req.params.id