best way to tell swaggerui where the host is

Here is what I do, since the loaded in document is just a JSON object:

var swaggerDoc = require('./api/swagger.json');
if (process.env.NODE_ENV === 'development') {
  swaggerDoc.host="localhost:" + process.env.PORT
}

// Initialize the Swagger middleware
swaggerTools.initializeMiddleware(swaggerDoc, function (middleware) {
  // Other initialization
}

This way you don't pollute your API specification with development environment configuration.


In recent versions of Swagger UI it's possible to do this, for example in onComplete:

window.swaggerUi.api.setHost("your.host:4242");


Swagger has a built-in json definition for host config, or can accept multiple inputs.

{
    "swagger": "2.0",
    "info": {
        "title": "Why API",
        "description": "Don't make that mistake again",
        "version": "0.0.1"
    },

    "host": "127.0.0.1:3000",
    "schemes": [
        "https"
    ]
}

Or

"host": "test.mydomain.com:3000",
"schemes": [
    "https"
],

Or you can have a dynamic host by defining a var and calling a hostname or machine name or other environment variables.

dynamic example

if (typeof this.host === 'undefined' || this.host === '') {
  this.host = location.host;
}
if (location.port) {
  this.host = this.host + ':' + location.port;
}

Tags:

Swagger Ui