apigee-127 / swagger-tools

A Node.js and browser module that provides tooling around Swagger.
MIT License
702 stars 371 forks source link

multiple swagger yaml spec in single server #599

Open Ashish2017 opened 5 years ago

Ashish2017 commented 5 years ago

I am trying to render 2 different yaml specs in same server. I am using nested middleware but both context paths('/docs/emc' and '/docs/mysales') rendering same yaml.

index.js goes like this:

var fs = require('fs'),
    path = require('path'),
    http = require('http');
var app = require('connect')();
var swaggerTools = require('swagger-tools');
var jsyaml = require('js-yaml');
var serverPort = 8084;

var specEmc = fs.readFileSync(path.join(__dirname, 'api/swaggerEmc.yaml'), 'utf8');
var swaggerDocEmc = jsyaml.safeLoad(specEmc);

var specMySales = fs.readFileSync(path.join(__dirname, 'api/swaggerMySales.yaml'), 'utf8');
var swaggerDocMySales = jsyaml.safeLoad(specMySales);

const swaggerUiOptionsEmc = {
    swaggerUi: '/docs/emc'
};

const swaggerUiOptionsMySales = {
    swaggerUi: '/docs/mysales'
};

swaggerTools.initializeMiddleware(swaggerDocEmc, function (middlewareEmc) {
    swaggerTools.initializeMiddleware(swaggerDocMySales, function (middlewareMySales) {
        app.use(middlewareEmc.swaggerMetadata());
        app.use(middlewareEmc.swaggerUi(swaggerUiOptionsEmc));

        app.use(middlewareMySales.swaggerMetadata());
        app.use(middlewareMySales.swaggerUi(swaggerUiOptionsMySales));
    });
});
http.createServer(app).listen(serverPort, function () {
});
whitlockjc commented 5 years ago

I don't think it was intended for this, I'll have to check.

Ashish2017 commented 5 years ago

Is there any other way to do this?