From 9f43d9cded2c5d6757b5a79e76f8c5ded6ed5bcf Mon Sep 17 00:00:00 2001 From: Yohan Lasorsa Date: Wed, 15 Mar 2017 04:05:53 +0100 Subject: [PATCH] Added option to display default Swagger UI (closes #38) (#40) --- README.md | 2 ++ src/index.js | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/README.md b/README.md index 54b48bc..8cfeaf5 100644 --- a/README.md +++ b/README.md @@ -227,6 +227,8 @@ Create a `docs.html` page like this: Now [localhost:3030/docs/](http://localhost:3030/docs/) will show the documentation in the browser using the Swagger UI. +You can also use `uiIndex: true` to use the default [Swagger UI](http://swagger.io/swagger-ui/). + ## License Copyright (c) 2016 diff --git a/src/index.js b/src/index.js index bdf03bc..612b66b 100644 --- a/src/index.js +++ b/src/index.js @@ -3,6 +3,10 @@ import url from 'url'; import serveStatic from 'serve-static'; import * as utils from './utils'; +// Find node_modules root path +let modulesRootPath = require.resolve('swagger-ui'); +modulesRootPath = modulesRootPath.substr(0, modulesRootPath.lastIndexOf('node_modules')); + export default function init (config) { return function () { const app = this; @@ -46,6 +50,13 @@ export default function init (config) { config.uiIndex(req, res); } else if (typeof config.uiIndex === 'string') { res.sendFile(config.uiIndex); + } else if (config.uiIndex === true) { + if (req.query.url) { + res.sendFile(path.join(modulesRootPath, 'node_modules/swagger-ui/dist/index.html')); + } else { + // Set swagger url (needed for default UI) + res.redirect('?url=' + encodeURI(config.docsPath)); + } } else { res.json(rootDoc); }