Skip to content

Commit

Permalink
Create docs route
Browse files Browse the repository at this point in the history
  • Loading branch information
hagopj13 committed May 12, 2020
1 parent 50620dc commit cb2c8fe
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 32 deletions.
21 changes: 21 additions & 0 deletions src/docs/swaggerDef.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const { version } = require('../../package.json');
const config = require('../config/config');

const swaggerDef = {
openapi: '3.0.0',
info: {
title: 'node-express-mongoose-boilerplate API documentation',
version,
license: {
name: 'MIT',
url: 'https://github.com/hagopj13/node-express-mongoose-boilerplate/blob/master/LICENSE',
},
},
servers: [
{
url: `http://localhost:${config.port}/v1`,
},
],
};

module.exports = swaggerDef;
21 changes: 21 additions & 0 deletions src/routes/v1/docs.route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const express = require('express');
const swaggerJsdoc = require('swagger-jsdoc');
const swaggerUi = require('swagger-ui-express');
const swaggerDefinition = require('../../docs/swaggerDef');

const router = express.Router();

const specs = swaggerJsdoc({
swaggerDefinition,
apis: ['src/docs/*.yml', 'src/routes/v1/*.js'],
});

router.use('/', swaggerUi.serve);
router.get(
'/',
swaggerUi.setup(specs, {
explorer: true,
})
);

module.exports = router;
34 changes: 2 additions & 32 deletions src/routes/v1/index.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,12 @@
const express = require('express');
const swaggerJsdoc = require('swagger-jsdoc');
const swaggerUi = require('swagger-ui-express');
const authRoute = require('./auth.route');
const userRoute = require('./user.route');
const docsRoute = require('./docs.route');

const router = express.Router();

router.use('/auth', authRoute);
router.use('/users', userRoute);

// Swagger set up
const options = {
swaggerDefinition: {
openapi: '3.0.0',
info: {
title: 'API Documentation',
version: '1.0.0',
description: 'Documentation discription',
license: {
name: 'MIT',
url: 'https://github.com/hagopj13/node-express-mongoose-boilerplate/blob/master/LICENSE',
},
},
servers: [
{
url: 'http://localhost:3000/api/v1',
},
],
},
apis: ['*.js'],
};
const specs = swaggerJsdoc(options);
router.use('/docs', swaggerUi.serve);
router.get(
'/docs',
swaggerUi.setup(specs, {
explorer: true,
})
);
router.use('/docs', docsRoute);

module.exports = router;

0 comments on commit cb2c8fe

Please sign in to comment.