use multiple api docs for swagger in node js?

Related searches

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

Documentation Portals for Multiple APIs, Run npm install swagger-ui to install swagger-ui to the project: Open /src/App.js; Add swagger-ui under the import statements at the top of the� The Swagger UI. The builder automatically creates the /api-docsendpoint where the JSON description is available. Using that, Swagger has an online viewer where users can try your API in just a couple of clicks. Here anyone can view your API definition, try those POSTs PUTs and DELETEs on the page live.

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

Splitting your swagger spec into multiple files in a Node project , Documenting your node project with multiple yaml file. import swaggerDefinitions swaggerDefinition, // path to the API docs apis: ['. use swagger-Ui-express for your app documentation endpoint app.use('/docs',� This means that above each existing API route, you write in your Swagger specs YAML-style (so indentation and colons count) for that route with the @swagger annotation, and this module will then translate that into the UI rendered by my second module: Swagger UI Express.

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

Can we have a multiple swagger UI? � Issue #193 � swagger-api , I'm developing a system using swagger-node-express and it works a swagger UI, on my localhost, its address is http://localhost:2052/docs I� A recent NodeJS API project came my way which had out-of-date OpenAPI 3 documentation for the few endpoints it already had, but the understanding that we where going to start using it a lot more

Splitting your swagger spec into multiple files in a Node project, Splitting your swagger spec into multiple files in a Node project In the swagger- doc directory, run npm init. Follow the instruction to set up the package.json file. Are you looking to accelerate your career as a developer? The swagger module provides tools for designing and building Swagger-compliant APIs entirely in Node.js. It integrates with popular Node.js servers, including Express, Hapi, Restify, and Sails, as well as any Connect-based middleware. With swagger, you can specify, build, and test your API from the very beginning, on your laptop. It allows you

Setting Up Swagger to API Test In a JavaScript Application, Spring Boot makes Swagger APIs look easy, Node.js not so much. Swagger UI Express documentation is much less verbose and involved� A good documentation is crucial for any REST API. Swagger is a tool that can help in both creating and displaying such a documentation.

As said before Swagger is supported in node.js through its own module, here is the link to the github project: The documentation is pretty straight-forward to help beginners understand and configure Swagger. Once we open the command line we install the module (global). npm install-g swagger Now let's create our project.

Comments
  • Possible duplicate of Swagger-ui-express module, instantiates only the last defined document
  • The CSS works fine for you on Swagger? I tried everything but the page served comes without styles.