0
0
Expressframework~10 mins

Swagger UI integration in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Swagger UI integration
Start Express App
Import swagger-ui-express
Load swagger.json
Use swagger-ui middleware
Start server
Access /api-docs in browser
Swagger UI renders API docs
This flow shows how an Express app loads Swagger UI middleware to serve API docs at /api-docs.
Execution Sample
Express
import express from 'express';
import swaggerUi from 'swagger-ui-express';
import swaggerDocument from './swagger.json' assert { type: 'json' };

const app = express();
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
app.listen(3000);
This code sets up Swagger UI middleware in an Express app to serve API docs at /api-docs.
Execution Table
StepActionEvaluationResult
1Import expressModule loadedexpress object ready
2Import swagger-ui-expressModule loadedswaggerUi object ready
3Import swagger.jsonJSON file parsedswaggerDocument object ready
4Create express appapp initializedapp object ready
5Use swagger-ui middlewareapp.use calledMiddleware registered at /api-docs
6Start server on port 3000app.listen calledServer listening on port 3000
7Browser requests /api-docsMiddleware triggeredSwagger UI page served
8Swagger UI rendersswaggerDocument usedAPI docs visible in browser
9ExitNo more stepsProcess running, waiting for requests
💡 Server runs indefinitely, serving Swagger UI at /api-docs until stopped
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5After Step 6
expressundefinedmodule loadedmodule loadedmodule loadedmodule loaded
swaggerUiundefinedmodule loadedmodule loadedmodule loadedmodule loaded
swaggerDocumentundefinedJSON loadedJSON loadedJSON loadedJSON loaded
appundefinedundefinedexpress app createdmiddleware addedserver listening
Key Moments - 3 Insights
Why do we import swagger.json as a JSON module instead of reading it as text?
Importing swagger.json as a JSON module parses it automatically, so swaggerDocument is a usable object for swaggerUi.setup, as shown in execution_table step 3.
What happens if we forget to use swaggerUi.serve middleware before swaggerUi.setup?
Without swaggerUi.serve, the static files for Swagger UI won't be served, so the docs page won't load properly. This is shown in execution_table step 5 where middleware is registered.
Why do we access /api-docs in the browser after starting the server?
Because the middleware is registered at /api-docs (step 5), accessing this path triggers Swagger UI to render the API docs (steps 7 and 8).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What is the state of swaggerDocument?
AIt is undefined because the file is not loaded yet
BIt is a parsed JSON object from swagger.json
CIt is a string containing JSON text
DIt is the express app instance
💡 Hint
Check execution_table row 3 under Result column
At which step does the Express app start listening for requests?
AStep 6
BStep 5
CStep 4
DStep 7
💡 Hint
Look at execution_table row 6 Action and Result
If we change the middleware path from '/api-docs' to '/docs', what changes in the execution?
ASwagger UI will not load at all
BThe server will fail to start
CSwagger UI will be served at /docs instead of /api-docs
DswaggerDocument will not be loaded
💡 Hint
Refer to execution_table step 5 where middleware path is registered
Concept Snapshot
Swagger UI integration in Express:
- Import express and swagger-ui-express modules
- Import swagger.json as JSON
- Create express app
- Use app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument))
- Start server with app.listen(port)
- Access http://localhost:port/api-docs to see API docs
Key: middleware serves Swagger UI static files and loads your API spec
Full Transcript
This visual execution trace shows how to integrate Swagger UI into an Express app. First, express and swagger-ui-express modules are imported. Then the swagger.json file is imported as a JSON object. An Express app is created. The Swagger UI middleware is registered at the '/api-docs' path using app.use. The server starts listening on port 3000. When a browser requests '/api-docs', the middleware serves the Swagger UI page, rendering the API documentation from swagger.json. Variables like express, swaggerUi, swaggerDocument, and app change state as the code runs. Key moments include understanding JSON import, middleware order, and accessing the correct URL. The quiz tests understanding of these steps and their effects.