swagger-ui-dist change url server side

Created on 7 Jun 2018  路  4Comments  路  Source: swagger-api/swagger-ui

I am following the installation document and using swagger-ui-dist in my Node.js project. The example is working fine but changing the url I need to edit node_modules/swagger-ui-dist/index.html. Is there a way to set the url from the express app? I don't want to lose the changes at the time of updates.

const express = require('express')
const pathToSwaggerUi = require('swagger-ui-dist').absolutePath()

const app = express()

app.use(express.static(pathToSwaggerUi))

app.listen(3000)

Q&A (please complete the following information)

  • OS: macOS
  • Browser: NA
  • Version: NA
  • Method of installation: npm
  • Swagger-UI version: 3.17.0
  • Swagger/OpenAPI version: OpenAPI 3.0

Content & configuration

Swagger/OpenAPI definition:

# your YAML here

Swagger-UI configuration options:

SwaggerUI({
  // your config options here
})
?yourQueryStringConfig

Screenshots

How can we help?

lock-bot support

Most helpful comment

Hi @sumitgoelpw,

Your intuition is correct - it's bad practice to tamper with files in node_modules 馃槃

A little bit of custom code can help you to modify the built-in index page:

const fs = require("fs")
const express = require("express")
const pathToSwaggerUi = require("swagger-ui-dist").absolutePath()

const indexContent = fs.readFileSync(`${pathToSwaggerUi}/index.html`)
  .toString()
  .replace("http://petstore.swagger.io/v2/swagger.json", "http://google.com/my/custom/url")

const app = express()

app.get("/", (req, res) => res.send(indexContent))
app.get("/index.html", (req, res) => res.send(indexContent)) // you need to do this since the line below serves `index.html` at both routes
app.use(express.static(pathToSwaggerUi))

app.listen(3000)

All 4 comments

Hi @sumitgoelpw,

Your intuition is correct - it's bad practice to tamper with files in node_modules 馃槃

A little bit of custom code can help you to modify the built-in index page:

const fs = require("fs")
const express = require("express")
const pathToSwaggerUi = require("swagger-ui-dist").absolutePath()

const indexContent = fs.readFileSync(`${pathToSwaggerUi}/index.html`)
  .toString()
  .replace("http://petstore.swagger.io/v2/swagger.json", "http://google.com/my/custom/url")

const app = express()

app.get("/", (req, res) => res.send(indexContent))
app.get("/index.html", (req, res) => res.send(indexContent)) // you need to do this since the line below serves `index.html` at both routes
app.use(express.static(pathToSwaggerUi))

app.listen(3000)

PS: If you have some extra time on your hands, a helper in swagger-ui-dist that abstracts this away would be awesome :)

Closing due to inactivity.

This is simply to keep our issue tracker clean - feel free to comment if there are any further thoughts or concerns, and we'll be happy to reopen this issue.

Locking due to inactivity.
This is done to avoid resurrecting old issues and bumping long threads with new, possibly unrelated content.
If you think you're experiencing something similar to what you've found here: please open a new issue, follow the template, and reference this issue in your report.
Thanks!

Was this page helpful?
0 / 5 - 0 ratings