Swagger UI reading YAML file

You’ve been playing with Swagger and can now see the benefits, and you are planning to use a YAML file, now you would like to display it to the outside work, here is a short article explaining all that is required.  This is based on you using Express within Node.js

Swagger UI Express

Adds middleware to your express app to serve the Swagger UI bound to your Swagger document. This acts as living documentation for your API hosted from within your app.

Swagger version is pulled from npm module swagger-ui-dist. Please use a lock file or specify the version of swagger-ui-dist you want to ensure it is consistent across environments.


Install using npm:

$ npm install swagger-ui-express

Load swagger from yaml file

To load your swagger specification yaml file you need to use a module able to convert yaml to json ; for instance yamljs.

npm install --save yamljs
const express = require('express');
const app = express();
const swaggerUi = require('swagger-ui-express');
const YAML = require('yamljs');
const swaggerDocument = YAML.load('./swagger.yaml');
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
A good example of Node.js using swagger can be found here:
For many, more options take a look here