swagger-jsdoc helps you create clear API documentation automatically from your code comments. It makes your API easy to understand for others.
swagger-jsdoc setup in Express
const swaggerJSDoc = require('swagger-jsdoc'); const options = { definition: { openapi: '3.0.0', info: { title: 'API Title', version: '1.0.0', description: 'API description here', }, }, apis: ['./routes/*.js'], }; const swaggerSpec = swaggerJSDoc(options);
definition holds the main API info and OpenAPI version.
apis is an array of file paths where swagger-jsdoc looks for comments.
app.js for API comments.const swaggerJSDoc = require('swagger-jsdoc'); const options = { definition: { openapi: '3.0.0', info: { title: 'My API', version: '1.0.0' }, }, apis: ['./app.js'], }; const swaggerSpec = swaggerJSDoc(options);
const swaggerJSDoc = require('swagger-jsdoc'); const options = { definition: { openapi: '3.0.0', info: { title: 'User API', version: '2.0.0', description: 'User management endpoints', }, }, apis: ['./routes/users.js', './routes/auth.js'], }; const swaggerSpec = swaggerJSDoc(options);
This example sets up an Express server with swagger-jsdoc and swagger-ui-express. It documents a simple /hello GET endpoint that returns a greeting. The Swagger UI is available at /api-docs.
const express = require('express'); const swaggerJSDoc = require('swagger-jsdoc'); const swaggerUi = require('swagger-ui-express'); const app = express(); const options = { definition: { openapi: '3.0.0', info: { title: 'Simple API', version: '1.0.0', description: 'A simple Express API documented with swagger-jsdoc', }, }, apis: ['./index.js'], }; const swaggerSpec = swaggerJSDoc(options); /** * @openapi * /hello: * get: * summary: Returns a greeting message * responses: * 200: * description: Greeting message * content: * text/plain: * schema: * type: string * example: Hello, world! */ app.get('/hello', (req, res) => { res.send('Hello, world!'); }); app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec)); app.listen(3000, () => { console.log('Server running on http://localhost:3000'); console.log('Swagger docs at http://localhost:3000/api-docs'); });
Make sure your API comment blocks use the correct @openapi or Swagger syntax for swagger-jsdoc to parse them.
Use swagger-ui-express to serve the generated docs in a friendly web page.
Keep the apis paths updated to include all files with API comments.
swagger-jsdoc generates API docs from comments in your code.
It needs a config object with API info and file paths.
Use swagger-ui-express to show docs in the browser.
Practice
swagger-jsdoc in an Express app?Solution
Step 1: Understand swagger-jsdoc role
swagger-jsdoc reads specially formatted comments in your Express code to create API documentation automatically.Step 2: Compare with other options
Handling HTTP requests is Express's job, not swagger-jsdoc. Database connection and serving static files are unrelated tasks.Final Answer:
To generate API documentation from comments in your code -> Option CQuick Check:
swagger-jsdoc = API docs generator [OK]
- Confusing swagger-jsdoc with Express routing
- Thinking swagger-jsdoc manages databases
- Assuming swagger-jsdoc serves static files
swagger-jsdoc in an ES modules-based Express app?Solution
Step 1: Use modern ES module import
In modern Express apps using ES modules,import swaggerJSDoc from 'swagger-jsdoc'is correct.Step 2: Initialize swagger-jsdoc with options
swaggerJSDoc requires an options object to generate specs, so calling it without arguments is incorrect.Final Answer:
import swaggerJSDoc from 'swagger-jsdoc'; const specs = swaggerJSDoc(options); -> Option AQuick Check:
Import + call with options = C [OK]
- Calling swaggerJSDoc() without options
- Using require instead of import in ES modules
- Not assigning the result to a variable
specs contain after calling swaggerJSDoc(options)?
const options = {
definition: {
openapi: '3.0.0',
info: { title: 'Test API', version: '1.0.0' }
},
apis: ['./routes/*.js']
};
const specs = swaggerJSDoc(options);Solution
Step 1: Understand swagger-jsdoc output
swagger-jsdoc returns a JSON object with OpenAPI info and paths parsed from specified files.Step 2: Check options correctness
The options specify OpenAPI version and info, and a valid path pattern for API files, so it will parse comments there.Final Answer:
An object with OpenAPI info and paths extracted from comments in './routes/*.js' -> Option BQuick Check:
swaggerJSDoc(options) = OpenAPI JSON object [OK]
- Expecting raw code instead of parsed JSON
- Assuming error without checking file existence
- Thinking output is empty without comments
import swaggerJSDoc from 'swagger-jsdoc';
import swaggerUi from 'swagger-ui-express';
const options = {
definition: {
openapi: '3.0.0',
info: { title: 'API', version: '1.0' }
},
apis: ['./api/*.js']
};
const specs = swaggerJSDoc();
app.use('/docs', swaggerUi.serve, swaggerUi.setup(specs));Solution
Step 1: Check swaggerJSDoc call
The code callsswaggerJSDoc()without arguments, but it requires the options object to generate specs.Step 2: Verify other parts
The 'apis' path is valid for JavaScript files. swaggerUi.setup() after serve is correct usage.Final Answer:
swaggerJSDoc is called without passing the options object -> Option AQuick Check:
Call swaggerJSDoc(options) not empty () [OK]
- Calling swaggerJSDoc() without options
- Confusing order of swaggerUi middleware
- Ignoring missing imports
routes/users.js and routes/products.js (assuming there are other .js files in the routes directory that should not be documented). Which apis option correctly includes both files for swagger-jsdoc?Solution
Step 1: Understand 'apis' option format
The 'apis' option is an array of file paths or glob patterns to scan for comments.Step 2: Check each option
['./routes/users.js', './routes/products.js'] lists both files explicitly, which is correct. ['./routes/*.js'] uses a glob that includes both files but may include others. ['./routes/users.js|./routes/products.js'] uses an invalid string with '|'. ['./routes/users.js', './routes/products'] has a missing file extension for products.js.Final Answer:
['./routes/users.js', './routes/products.js'] -> Option DQuick Check:
List each file path separately in array [OK]
- Using invalid string with '|' instead of array items
- Omitting file extensions
- Using too broad glob patterns unintentionally
Ready to prove it?
Pass the test and this pattern moves unknown
