简体   繁体   中英

use multiple api docs for swagger in node js?

I have given versioned my API for a project. so it has two folders v1 and v2 which has diffrent apis. Now in ordrer to implement swagger for v1 and v2 i have written below code in 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 . so the last line written here for v2 so it always showing doc for v2 only. Please suggests how to support multiple api?

This is the Known issue in the 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

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) });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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