首页 > 解决方案 > 如何在nodejs应用程序的swagger-ui中指定多个基本路径

问题描述

我有一个在端口 8080 上运行的 Node.js 服务器。我正在使用 swagger,它允许设置基本路径。我想为不同的端点设置多个基本路径。我正在使用swagger-ui-express服务器招摇 ui 文件。

我浏览了 swagger 的文档并尝试使用服务器属性,但它没有用。我的端点是:

  1. 端点:/users/movies
    应作为:http://localhost:9090/cinema-mgr-api/users/movies
  2. 端点:/aws-mgr-api/storage
    应作为:http://localhost:9090/aws-mgr-api/storage

如何定义多个基本路径swagger-ui-express

标签: node.jsswagger-uiendpoint

解决方案


下面的代码是我的项目。我已经使用了swagger-tools依赖项,版本是0.10.4.

招摇.json

{
    "swagger": "2.0",
    "info": {
        "title": "Node JS APP",
        "description": "Node JS APP",
        "version": "1.0.0"
    },
    "produces": [
        "application/json"
    ],
    "host": "localhost:9000",
    "basePath": "/api",
    "paths": {
        "/cinema-mgr-api/users/movies":{
            "post": {
                "tags": ["controller"],
                "description": "movies",
                "x-swagger-router-controller": "controller",
                "operationId": "movies",
                "parameters": [
                    {
                        "name": "body",
                        "in": "body",
                        "description": "movies",
                        "required": true,
                        "schema": {
                            "$ref": "#/definitions/movies"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "search successful"
                    }
                }
            }
        },
        "/aws-mgr-api/storage": {
            "post": {
                "tags": ["aws-controller"],
                "description": "storage",
                "x-swagger-router-controller": "aws-controller",
                "operationId": "storage",
                "parameters": [
                    {
                        "name": "body",
                        "in": "body",
                        "required": true,
                        "schema": {
                            "$ref": "#/definitions/storage"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "stored Successfully"
                    }
                }
            }
        }
    },
    "definitions": {
        "movies": {
            "type": "object",
            "required": [
                "movieName"
            ],
            "properties": {
                "movieId": {
                    "type": "string"
                },
                "movieName": {
                    "type": "string"
                },
                "filters": {
                    "type": "object",
                    "properties": {
                        "period": {
                            "type": "string"
                        },
                        "year": {
                            "type": "array"
                        },
                        "language": {
                            "type": "array"
                        }
                    }
                }
            }
        },
        "storage": {
            "type":"object"
        }
    }
}

推荐阅读