question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Integrate Swagger-UI into project

See original GitHub issue

I followed these instructions; probably the best Instructions i’ve seen anywhere pertaining to swagger (and clearest), https://idratherbewriting.com/learnapidoc/pubapis_swagger.html#create-a-swagger-ui-display When i run the nodejs project on my local machine it runs fine; i can go to the ‘/swagger’ route and see the swagger-ui. However when i publish this to the server it can’t find the ‘page’.

Per the instruction above, i am supposed to do the following:

  1. download the dist folder (https://github.com/swagger-api/swagger-ui)
  2. copy the contents of the dist folder into your projects (copied all files into the project directory where the project swagger.yaml file existed … ./app/swagger/swagger.yaml)
  3. modify the content of the index.html file so it points to your swagger.yaml file: // Build a system const ui = SwaggerUIBundle({ url: "swagger.yaml", // <-- modified this line dom_id: '#swagger-ui', docExpansion: 'none', deepLinking: true, presets: [ SwaggerUIBundle.presets.apis, SwaggerUIStandalonePreset ], plugins: [ SwaggerUIBundle.plugins.DownloadUrl ], layout: "StandaloneLayout" })
  4. in the app.js file include the following code (route to swagger UI) //app.use(‘/swagger’, express.static(‘./api/swagger’)); var path = require(‘path’); app.use(‘/swagger’, express.static(path.join(__dirname + ‘/api/swagger’)));

I’ll probably end up muddling through this on my own but doesn’t kill to post the question. Reading articles online is at times minefield because you find too many ways to do the same thing and it’s also at times difficult to tell what resources (node package) are needed.

I am guessing either step #3 or #4 is wrong or i am missing an node package. The swagger file itself works fine when tested with the swagger editor (is valid). If i figure out the issue i’ll post it.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
fractalocitycommented, Oct 2, 2017

You are right. I went ahead and switched the forward slash to a double back slash and published to the server and it works. I’ve been working a good portion of the day testing out all the wrong things. The final solution was:

  1. download the zip folder from (https://github.com/swagger-api/swagger-ui)
  2. with the zip file you will find a ‘dist’ folder. copy all files from the ‘dist’ folder into your project (into the folder/directory where your swagger.yaml file is located)
  3. open the index.html file and change the ‘url’ in the following code to point to your swagger file const ui = SwaggerUIBundle({ url: "swagger.yaml", // <-- modified this line **** dom_id: '#swagger-ui', docExpansion: 'none', deepLinking: true, presets: [ SwaggerUIBundle.presets.apis, SwaggerUIStandalonePreset ], plugins: [ SwaggerUIBundle.plugins.DownloadUrl ], layout: "StandaloneLayout" })
  4. go to your app.js file and type in the following line app.use(‘/swagger’, express.static(‘api\swagger\’)); note: step 4 assumes the following directory structure -> Project_folder/app.js -> Project_folder/api/swagger/<swagger.yaml & all files from dist folder here!>
  5. run your node.js project: if you type ‘/swagger’ after the base path, you should now see the swagger UI (ex: http://localhost:1337/swagger/)

Again most of this came from the website mentioned above … just added some to the explanation. Thanks again Shockey for all the help. Helped out a ton.

1reaction
shockeycommented, Jun 26, 2018

@brianupskill you’d need to consult a lawyer to discuss the specifics of your use case - we can’t provide that for you here 😄

More generally, here’s a plain-English summary of the Apache 2.0 license: https://tldrlegal.com/license/apache-license-2.0-(apache-2.0)

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to add Swagger UI to an existing Node.js and Express.js ...
How to add Swagger UI to an existing Node.js and Express.js project ... Open the server.js file and add the below code for...
Read more >
How To Add Swagger To NodeJS REST API - Medium
The first thing is to add the file swagger.json to define the operations. You can define schemas for the request and response objects,...
Read more >
REST API Documentation Tool | Swagger UI
Write and visualize new API definitions or import your existing OAS definitions into SwaggerHub to generate an interactive UI, fully-hosted in the cloud....
Read more >
Integrating Swagger UI with the rest of your docs
It's pretty easy to embed Swagger into an HTML page — just copy the code from the index.html file from the dist folder...
Read more >
How to implement and use Swagger in Node.js - In Plain English
Adding Swagger to the project # ... And I am importing these two modules in the index.js file: import swaggerUI from “swagger-ui-express”; import ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found