简体   繁体   English

使用多个 api 文档在节点 js 中大摇大摆?

[英]use multiple api docs for swagger in node js?

I have given versioned my API for a project.我已经为一个项目提供了我的 API 版本。 so it has two folders v1 and v2 which has diffrent apis.所以它有两个文件夹 v1 和 v2,它们有不同的 api。 Now in ordrer to implement swagger for v1 and v2 i have written below code in app.js .现在为了实现 v1 和 v2 的 swagger,我在app.js 中编写了下面的代码。

// Swagger definition
// You can set every attribute except paths and swagger
const swaggerDefinition = {
    swagger: '2.0',
    info: {
        // API informations (required)
        title: 'API', // Title (required)
        version: '1.0.0', // Version (required)
        description: 'Used for  api documentation', // Description (optional)
    },
    host: `localhost:3000`, // Host (optional)
    basePath: '/v1', // Base path (optional)
};

// Options for the swagger docs
const optionsV1 = {
    // Import swaggerDefinitions
    swaggerDefinition,
    // Path to the API docs
    // Note that this path is relative to the current directory from which the Node.js is ran, not the application itself.
    apis: ['./app/v1/docs/*.yaml']
};

const optionsV2 = {
    // Import swaggerDefinitions
    swaggerDefinition,
    // Path to the API docs
    // Note that this path is relative to the current directory from which the Node.js is ran, not the application itself.
    apis: ['./app/v2/docs/*.yaml']
};
optionsV2.swaggerDefinition.basePath = "/v2"
// Initialize swagger-jsdoc -> returns validated swagger spec in json format
const swaggerSpecV1 = swaggerJSDoc(optionsV1);
const swaggerSpecV2 = swaggerJSDoc(optionsV2);
// const swaggerDocument = require('./app/v1/docs/swagger.json');
// app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
app.use('/v1/docs', swaggerUi.serve, swaggerUi.setup(swaggerSpecV1));
app.use('/v2/docs', swaggerUi.serve, swaggerUi.setup(swaggerSpecV2));

but the if i hit url as /v1/docs or /v2/docs it always show me the api doc for v2 .但是如果我点击 url 作为/v1/docs/v2/docs它总是向我显示v2的api doc。 so the last line written here for v2 so it always showing doc for v2 only.所以最后一行是为v2写的,所以它总是只显示 v2 的文档。 Please suggests how to support multiple api?请建议如何支持多个api?

This is the Known issue in the Swagger UI.这是 Swagger UI 中的已知问题。 Please use the following format to route the request:请使用以下格式来路由请求:

var swaggerHtml = swaggerUi.generateHTML(swaggerDocument, swaggerUiOpts)
app.use('/api-docs-html1', swaggerUi.serveFiles(swaggerDocument, swaggerUiOpts))
app.get('/api-docs-html1', (req, res) => { res.send(swaggerHtml) });

Updated Code:更新代码:

var swaggerHtmlV1 = swaggerUi.generateHTML(swaggerSpecV1, optionsV1)
var swaggerHtmlV2 = swaggerUi.generateHTML(swaggerSpecV2, optionsV2)

app.use('/v1/docs', swaggerUi.serveFiles(swaggerSpecV1, optionsV1))
app.get('/v1/docs', (req, res) => { res.send(swaggerHtmlV1) });

app.use('/v2/docs', swaggerUi.serveFiles(swaggerSpecV2, optionsV2))
app.get('/v2/docs', (req, res) => { res.send(swaggerHtmlV2) });

Please check the following link for more details: https://github.com/scottie1984/swagger-ui-express/issues/65请查看以下链接了解更多详情: https : //github.com/scottie1984/swagger-ui-express/issues/65

Try the following configuration:尝试以下配置:

app.use('/v1/docs', swaggerUi.serve, (...args) => swaggerUI.setup(swaggerSpecV1)(...args));
app.use('/v2/docs', swaggerUi.serve, (...args) => swaggerUI.setup(swaggerSpecV2)(...args));

Converted my code as per answer根据答案转换我的代码

var swaggerHtmlV1 = swaggerUi.generateHTML(swaggerSpecV1, optionsV1)
var swaggerHtmlV2 = swaggerUi.generateHTML(swaggerSpecV2, optionsV2)

app.use('/v1/docs', swaggerUi.serveFiles(swaggerSpecV1, optionsV1))
app.get('/v1/docs', (req, res) => { res.send(swaggerHtmlV1) });

app.use('/v2/docs', swaggerUi.serveFiles(swaggerSpecV2, optionsV2))
app.get('/v2/docs', (req, res) => { res.send(swaggerHtmlV2) });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM