Example of Express with swagger-jsdoc
$ git clone https://github.com/SangHakLee/swagger-express-jsdoc.git
$ npm i swagger-express-jsdoc
$ cd node_modules
$ cd swagger-express-jsdoc
$ npm install
$ npm start
http://localhost:3000/api-docs/
This application is based on Swagger, swagger-jsdoc and swagger-ui-express
// ...
var swaggerUi = require('swagger-ui-express'); // line 7
var swaggerJSDoc = require('swagger-jsdoc'); // line 8
// ...
var options = { // line 27
swaggerDefinition: {
info: {
title: 'swagger-express-jsdoc', // Title (required)
version: '1.0.0', // Version (required)
},
},
apis: ['./routes/*'], // Path to the API docs
};
var swaggerSpec = swaggerJSDoc(options); // line 36
// ...
app.get('/api-docs.json', function(req, res) { // line 41
res.setHeader('Content-Type', 'application/json');
res.send(swaggerSpec);
});
// ...
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec)); // line 45
Your Swagger docs information here. swagger-jsdoc
{
"title": "Swagger Sample App",
"description": "This is a sample server Petstore server.",
"termsOfService": "http://swagger.io/terms/",
"contact": {
"name": "API Support",
"url": "http://www.swagger.io/support",
"email": "support@swagger.io"
},
"license": {
"name": "Apache 2.0",
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
},
"version": "1.0.1"
}
Your routing files path. If your routing file path like this,
- /controllers/users.js
- /controllers/stores.js
Set your apis
value like this, apis: ['./controllers/*']
It will be json object.
Your option
value and apis
value are combined.
This returns a simple json document. http://localhost:3000/api-docs.json
This will convert the json document to Swagger-ui. So when you connect with http://localhost:3000/api-docs, it will make you see beautiful documents.