turivishal
turivishal

Reputation: 36144

How to access swagger api docs from one project to another project in nodejs and express?

Swagger Implementation:

const express = require('express');
const router = express.Router();
const swaggerUi = require('swagger-ui-express');
const swaggerJSDoc = require('swagger-jsdoc');
module.exports = async (app) => {
    router.use('/', swaggerUi.serve, swaggerUi.setup(
        swaggerJSDoc({
            swaggerDefinition: { ...require('../swagger.json') },
            apis: ['./app/**/*.js']
        }),
        {
            swaggerOptions: {
                displayRequestDuration: true,
                docExpansion: "none", 
                filter: false,
                showExtensions: true,
                showCommonExtensions: true,
                displayOperationId: true
            }
        }
    ));
    app.use('/api-docs', router);  // SET SWAGGER DOCS
}

I have 2 nodejs-express projects and implemented swagger as per above code:

  1. First Project running in http:localhost:3001
  2. Second project running in http:localhost:3002

I have third project only for swagger docs, that is running in http:localhost:3000, I want to access both (1), (2) of the projects docs in this third project using explorer for only swagger api docs, but i can not able to access it direct by url of project,

    router.use('/', swaggerUi.serve, swaggerUi.setup(
        swaggerJSDoc({
            swaggerDefinition: { ...require('../swagger.json') }
        }),
        {
            explorer: true,
            swaggerOptions: {
                displayRequestDuration: true,
                docExpansion: "none", 
                filter: false,
                showExtensions: true,
                showCommonExtensions: true,
                displayOperationId: true,
                urls: [
                    {
                        url: 'http://localhost:3001',
                        // url: 'http://localhost:3001/api-docs', // also tried but not working
                        name: 'Project 1'
                    },
                    {
                        url: 'http://localhost:3002',
                        // url: 'http://localhost:3002/api-docs', // also tried but not working
                        name: 'Project 2'
                    }
                ]
            }
        }
    ));
    app.use('/api-docs', router); // SET SWAGGER DOCS

It says failed to load api definition.

Am i doing wrong way or is there any other npm i have to implement for this?

Upvotes: 2

Views: 1241

Answers (1)

turivishal
turivishal

Reputation: 36144

I just learned about to set route in app.get method and send response as swaggerJSDoc, got little help from this question

const definition = swaggerJSDoc({
    swaggerDefinition: { ...require('../swagger.json') },
    apis: ['./app/**/*.js']
});
app.get('/api-docs.json', function (req, res) {
    res.header("Content-Type", "application/json");
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");      
    res.send(definition);
});

Now i am able to access both projects docs using "http://localhost:3001/api-docs.json" and "http://localhost:3002/api-docs.json"

Upvotes: 1

Related Questions