0
0
Expressframework~10 mins

swagger-jsdoc setup in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - swagger-jsdoc setup
Import swagger-jsdoc
Define swagger options
Generate swaggerSpec with swagger-jsdoc
Use swaggerSpec in Express app
Serve Swagger UI with generated docs
This flow shows how to import swagger-jsdoc, define options, generate docs, and serve them in an Express app.
Execution Sample
Express
const swaggerJSDoc = require('swagger-jsdoc');

const options = {
  definition: { openapi: '3.0.0', info: { title: 'API', version: '1.0.0' } },
  apis: ['./routes/*.js']
};

const swaggerSpec = swaggerJSDoc(options);
This code imports swagger-jsdoc, sets options with API info and files, then generates the swaggerSpec object.
Execution Table
StepActionInput/StateOutput/State
1Import swagger-jsdocNoneswaggerJSDoc function available
2Define options objectSet openapi version, info, apis pathoptions object created
3Call swaggerJSDoc(options)options objectswaggerSpec object generated with API docs
4Use swaggerSpec in ExpressswaggerSpec objectSwagger UI can serve API docs
5Serve Swagger UI routeExpress app configuredAPI docs accessible at /api-docs
💡 Swagger docs are ready and served in Express app
Variable Tracker
VariableStartAfter Step 2After Step 3Final
swaggerJSDocundefinedfunction importedfunction importedfunction imported
optionsundefined{definition, apis}{definition, apis}{definition, apis}
swaggerSpecundefinedundefinedgenerated swaggerSpec objectgenerated swaggerSpec object
Key Moments - 3 Insights
Why do we need to specify the 'apis' path in options?
The 'apis' path tells swagger-jsdoc where to find the API route files with comments to generate docs, as shown in step 2 of the execution_table.
What does swagger-jsdoc(options) return?
It returns a swaggerSpec object containing the OpenAPI documentation generated from the options and API comments, as seen in step 3.
How do we make the generated docs visible in the Express app?
By using the swaggerSpec object with a Swagger UI middleware route in Express, as described in steps 4 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output after step 3?
Aswagger-jsdoc function imported
Boptions object with API paths
CswaggerSpec object with API docs
DExpress app configured
💡 Hint
Check the 'Output/State' column for step 3 in execution_table
At which step do we define the API info like title and version?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the 'Action' and 'Input/State' columns in execution_table for where options are created
If we change the 'apis' path in options, what changes in the execution?
Aswagger-jsdoc function will not import
BswaggerSpec will include docs from new API files
CExpress app will crash
DNo change in swaggerSpec
💡 Hint
Refer to the role of 'apis' in step 2 and 3 of execution_table
Concept Snapshot
swagger-jsdoc setup:
1. Import swagger-jsdoc
2. Define options with OpenAPI info and API file paths
3. Generate swaggerSpec by calling swaggerJSDoc(options)
4. Use swaggerSpec in Express to serve docs
5. Access docs via Swagger UI route
Full Transcript
This visual execution shows how to set up swagger-jsdoc in an Express app. First, you import the swagger-jsdoc library. Then, you create an options object that includes OpenAPI version, API information like title and version, and the paths to your API route files. Next, you call swaggerJSDoc with these options to generate a swaggerSpec object containing your API documentation. Finally, you use this swaggerSpec in your Express app to serve the Swagger UI, making your API docs accessible to users. The execution table traces each step, showing how variables change and how the setup progresses until the docs are ready to serve.