Skip to content

Instantly share code, notes, and snippets.

@rocioDEV
Created February 15, 2020 19:32
Show Gist options
  • Save rocioDEV/dcd21717a2e51aba46d358bde6e44d06 to your computer and use it in GitHub Desktop.
Save rocioDEV/dcd21717a2e51aba46d358bde6e44d06 to your computer and use it in GitHub Desktop.
Express live documentation with swagger-jsdoc & swagger-ui-express
// ... non relevant require statements are omitted
const swaggerJsdoc = require('swagger-jsdoc')
const swaggerUi = require('swagger-ui-express')
const packageJson = require('../package.json')
const appVersion = packageJson.version
const appName = packageJson.name
const API_DOCS_PATH = path.resolve(__dirname, '../api.yaml')
const app = express()
// Create specs object with swagger-jsdoc, it will read
// your yaml files / comments and parse them
const specs = swaggerJsdoc({
definition: {
openapi: '3.0.1',
info: {
title: appName,
version: appVersion,
description: 'Autogenerated OpenAPI 3.0.1 doc',
},
},
apis: [path.resolve(__dirname, '../api.yaml')],
})
// Pass parsed object to swagger-ui-express to expose
// this information thourgh and express endpoint
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(specs))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment