简体   繁体   中英

Node.js Swagger generate UI doc JSON feed

I'm using Node.js and Swagger to build a restful API. The Swagger UI (demo link here http://petstore.swagger.io/ ) requires a JSON feed.

How would you generate this feed based on the Swagger spec yaml file? Is there not a npm module that can iterate over the yaml file and generate the JSON feed? I'm new to Swagger so I might have overlooked something.

Swagger UI should support YAML by now. You can convert YAML to JSON using JSYAML library.

You can also use the Swagger Editor:

http://editor.swagger.io/#/

I can't say I have tried it before, but you can upload YAML and export as YAML or JSON.

I have used the editor before and do really like it.

EDIT:

So to setup the swagger ui I did the following:

var swaggerUi = new SwaggerUi({
  url:"http://petstore.swagger.io/v2/swagger.json",
  dom_id:"swagger-ui-container"
});

swaggerUi.load();

That comes from here:

https://github.com/swagger-api/swagger-ui

I think you got that part.

As for the doc (YAML or JSON) just serve it out from your site. If you are using express with your nodejs server just serve out the file statically:

http://expressjs.com/starter/static-files.html

app.use('/static', express.static('public'));

So in this case if you put the swagger.json file right under your public dir you can access it with:

http://<yoursite>/swagger.json

Just test that you can actually get to your swagger docs from that url in the browser. Put that URL into the 'url' parameter above when you setup swagger-ui and you should be go to go. Swagger-ui will do the rest for you, it will pull the json or yaml definition from your site and generate the interactive docs site.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM