Skip to content

Commit

Permalink
Added option to display default Swagger UI (closes #38) (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
sinedied authored and daffl committed Mar 15, 2017
1 parent 449894c commit 9f43d9c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 9f43d9c

Please sign in to comment.