Change default API spec URL in Swagger UI

2019-08-01 05:47发布

问题:

I'm using the Swagger Editor Docker image for defining my API specification and generate a nodejs server to run as stub api and serve the documentation for my API spec.

I would like to know if it is possible to change the default URL to my own in order to present my spec when I open the page, and how I would do that.

For instance, I would like to change the default address to http://localhost:8080/spec

回答1:

If I understand correctly, you used Swagger Codegen to generate a Node.js server. Now when you npm start and open Swagger UI, you want the input box (the spec URL) to display http://localhost:8080/spec instead of http://localhost/api-docs.

To change the path the .yaml spec is served from, edit your index.js as follows:

// 1 - add swaggerUi options
var uiOptions = {
  apiDocs: '/spec'  // <-- override the default /api-docs
};

swaggerTools.initializeMiddleware(swaggerDoc, function (middleware) {
  ...

  // 2 - pass these options to swaggerUi
  app.use(middleware.swaggerUi(uiOptions));

The port number is specified by the serverPort variable in index.js:

var serverPort = 8080;