Intermediate LevelExpress JS
How do you automatically generate interactive OpenAPI (Swagger) documentation from Express JSDoc comments?
❓ Interview Prompt: Explain OpenAPI 3.0 specification, swagger-jsdoc comment parsing, and mounting swagger-ui-express interactive docs.
Maintaining manual API documentation separate from code results in outdated docs over time.
Swagger JSDoc + Swagger UI Express:
1
'swagger-jsdoc': Parses YAML/JSDoc block comments ('@openapi') written directly above route handlers.
2
'swagger-ui-express': Mounts an interactive Swagger UI page at '/api-docs', enabling developers to test API endpoints interactively from the browser!
Code Implementation Example
javascript
import express from 'express';
import swaggerUi from 'swagger-ui-express';
import swaggerJsdoc from 'swagger-jsdoc';
const app = express();
const spec = swaggerJsdoc({
definition: { openapi: '3.0.0', info: { title: 'My API', version: '1.0.0' } },
apis: ['./server.js']
});
/**
* @openapi
* /api/users:
* get:
* description: Retrieve user list
* responses:
* 200:
* description: Success
*/
app.get('/api/users', (req, res) => res.json([]));
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(spec));💡Generating interactive OpenAPI Swagger documentation from JSDoc comments.
🎯 Interview Key Takeaway
Use swagger-jsdoc to parse inline OpenAPI comments and swagger-ui-express to serve interactive API documentation.
#Swagger#OpenAPI#Documentation#Intermediate
Back to all Express JS Questions→