├── .gitignore ├── README.md ├── app.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | package-lock.json 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # express-swagger-docs 2 | 3 | Tutorial on automatically generating & serving swagger-ui documentation based on jsdocs. 4 | 5 | ## Running Locally 6 | * `npm install` 7 | * `npm start` 8 | * In the browser go to http://localhost:5000/api-docs. -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | const express = require('express'); 2 | const app = express(); 3 | const swaggerJsDoc = require('swagger-jsdoc'); 4 | const swaggerUI = require('swagger-ui-express'); 5 | 6 | const swaggerOptions = { 7 | swaggerDefinition: { 8 | info: { 9 | title: "Library API", 10 | version: '1.0.0', 11 | }, 12 | }, 13 | apis: ["app.js"], 14 | }; 15 | 16 | const swaggerDocs = swaggerJsDoc(swaggerOptions); 17 | app.use('/api-docs', swaggerUI.serve, swaggerUI.setup(swaggerDocs)); 18 | 19 | /** 20 | * @swagger 21 | * /books: 22 | * get: 23 | * description: Get all books 24 | * responses: 25 | * 200: 26 | * description: Success 27 | * 28 | */ 29 | app.get('/books', (req, res) => { 30 | res.send([ 31 | { 32 | id: 1, 33 | title: "Harry Potter", 34 | } 35 | ]) 36 | }); 37 | 38 | /** 39 | * @swagger 40 | * /books: 41 | * post: 42 | * description: Get all books 43 | * parameters: 44 | * - name: title 45 | * description: title of the book 46 | * in: formData 47 | * required: true 48 | * type: string 49 | * responses: 50 | * 201: 51 | * description: Created 52 | */ 53 | app.post('/books', (req, res) => { 54 | res.status(201).send(); 55 | }); 56 | 57 | app.listen(5000, () => console.log("listening on 5000")); 58 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "express-swagger-docs", 3 | "version": "1.0.0", 4 | "description": "Automatically generate swagger docs based on jsdocs", 5 | "main": "app.js", 6 | "scripts": { 7 | "start": "node app.js" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "express": "^4.17.1", 13 | "swagger-jsdoc": "^4.0.0", 14 | "swagger-ui-express": "^4.1.4" 15 | }, 16 | "devDependencies": { 17 | "nodemon": "^2.0.4" 18 | } 19 | } 20 | --------------------------------------------------------------------------------