📖 Scenario: You are building a simple Express server for a small web service. You want to add interactive API documentation so that users and developers can easily explore your API endpoints.
🎯 Goal: Integrate Swagger UI into your Express server to serve API documentation from a JSON specification file.
📋 What You'll Learn
Create an Express app instance
Add a Swagger JSON specification object
Use the swagger-ui-express middleware to serve the Swagger UI
Mount the Swagger UI at the '/api-docs' route
💡 Why This Matters
🌍 Real World
API documentation is essential for developers to understand and test your web services easily. Swagger UI integration provides a user-friendly interface to explore your API.
💼 Career
Many backend and full-stack developer roles require setting up API documentation. Knowing how to integrate Swagger UI with Express is a valuable skill for building maintainable APIs.
Progress0 / 4 steps
1
Create the Express app
Create a variable called express by requiring the 'express' module. Then create a variable called app by calling express().
Express
Hint
Use require('express') to import Express and then call it as a function to create your app.
2
Add the Swagger JSON specification
Create a constant called swaggerDocument and assign it an object with a swagger property set to '2.0', an info object with title 'Sample API' and version '1.0.0', and a paths object with a single path '/hello' that has a get method with a responses object containing a 200 response with a description 'Successful response'.
Express
Hint
Define the Swagger JSON object exactly as described to describe your API.
3
Add swagger-ui-express middleware
Require the swagger-ui-express module into a constant called swaggerUi. Then use app.use to mount swaggerUi.serve and swaggerUi.setup(swaggerDocument) at the route '/api-docs'.
Express
Hint
Use require('swagger-ui-express') and app.use to serve the Swagger UI at '/api-docs'.
4
Start the Express server
Add a call to app.listen on port 3000 with a callback function that logs the string 'Server running on port 3000'.
Express
Hint
Use app.listen with a callback to start the server and log a message.
Practice
(1/5)
1. What is the main purpose of integrating swagger-ui-express in an Express app?
easy
A. To handle database connections automatically
B. To display API documentation in a user-friendly web page
C. To improve server performance by caching responses
D. To secure the API with authentication middleware
Solution
Step 1: Understand Swagger UI role
Swagger UI provides a visual interface for API documentation, making it easier to explore and test endpoints.
Step 2: Identify middleware purpose
swagger-ui-express middleware serves this UI in Express apps, not handling database or security tasks.
Final Answer:
To display API documentation in a user-friendly web page -> Option B
Quick Check:
Swagger UI = API docs display [OK]
Hint: Swagger UI shows API docs visually in browser [OK]
Common Mistakes:
Confusing Swagger UI with security middleware
Thinking it manages database connections
Assuming it improves server speed
2. Which of the following is the correct way to import and use swagger-ui-express in an Express app?
easy
A. const swaggerUi = require('swagger-ui-express'); app.listen('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
B. import swaggerUi from 'swagger-ui-express'; app.get('/api-docs', swaggerUi.render(swaggerDocument));
C. const swaggerUi = require('swagger-ui-express'); app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
D. import swaggerUi from 'swagger-ui-express'; app.use('/api-docs', swaggerUi.render, swaggerUi.setup(swaggerDocument));
Solution
Step 1: Check import style
Using CommonJS, require('swagger-ui-express') is correct for many Express apps.
Step 2: Verify middleware usage
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument)) correctly serves Swagger UI at the route.
Final Answer:
const swaggerUi = require('swagger-ui-express'); app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument)); -> Option C