Swagger UI helps you see and test your API in a simple web page. It makes understanding and using your API easier.
Swagger UI integration in 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, () => console.log('Server running on http://localhost:3000'));
Use swagger-ui-express package to serve Swagger UI in Express.
The Swagger document is usually a JSON file describing your API.
/docs URL path.import swaggerUi from 'swagger-ui-express'; import swaggerDocument from './swagger.json' assert { type: 'json' }; app.use('/docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument, { explorer: true }));This Express app has a simple /hello endpoint and serves Swagger UI at /api-docs. The Swagger JSON file describes the API endpoints.
import express from 'express'; import swaggerUi from 'swagger-ui-express'; import swaggerDocument from './swagger.json' assert { type: 'json' }; const app = express(); app.get('/hello', (req, res) => { res.json({ message: 'Hello, world!' }); }); app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument)); app.listen(3000, () => { console.log('Server running on http://localhost:3000'); console.log('Swagger UI available at http://localhost:3000/api-docs'); });
Make sure your Swagger JSON file is valid and describes your API correctly.
You can customize Swagger UI appearance and behavior with options in swaggerUi.setup().
Use browser developer tools to inspect the Swagger UI page and debug if needed.
Swagger UI integration shows your API in a friendly web page.
Use swagger-ui-express middleware in Express to serve it.
Keep your Swagger JSON updated to reflect your API changes.
Practice
swagger-ui-express in an Express app?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-expressmiddleware 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 BQuick Check:
Swagger UI = API docs display [OK]
- Confusing Swagger UI with security middleware
- Thinking it manages database connections
- Assuming it improves server speed
swagger-ui-express in an Express app?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 CQuick Check:
Correct import + middleware = const swaggerUi = require('swagger-ui-express'); app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument)); [OK]
- Using app.listen instead of app.use for middleware
- Calling render instead of serve and setup
- Mixing import and require incorrectly
/api-docs in the browser?const express = require('express');
const swaggerUi = require('swagger-ui-express');
const swaggerDocument = { openapi: '3.0.0', info: { title: 'Test API', version: '1.0.0' }, paths: {} };
const app = express();
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
app.listen(3000);Solution
Step 1: Analyze swaggerDocument content
The swaggerDocument defines basic API info with no paths, so UI will show API title but no endpoints.Step 2: Understand middleware effect
The middleware serves Swagger UI at/api-docs, so visiting that route shows the UI with given document.Final Answer:
You will see a Swagger UI page showing 'Test API' with no endpoints -> Option AQuick Check:
Swagger UI shows docs even if no paths [OK]
- Expecting 404 if no API paths defined
- Thinking server crashes without routes
- Assuming blank page means error
/api-docs shows a blank page. Which fix is most likely correct?Solution
Step 1: Identify correct middleware usage
Swagger UI requires bothswaggerUi.serveto serve static files andswaggerUi.setup(swaggerDocument)to load docs.Step 2: Understand common blank page cause
Missing either part causes blank page because UI assets or docs are not served properly.Final Answer:
Ensure you use both swaggerUi.serve and swaggerUi.setup(swaggerDocument) in app.use -> Option DQuick Check:
Serve + setup needed for Swagger UI [OK]
- Using app.get instead of app.use for middleware
- Omitting swaggerDocument in setup
- Changing port unrelated to UI display
Solution
Step 1: Check environment condition
Usingif (process.env.NODE_ENV === 'development')ensures Swagger UI middleware runs only in development.Step 2: Confirm middleware usage
Inside the condition,app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument))correctly sets up Swagger UI.Final Answer:
if (process.env.NODE_ENV === 'development') { app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument)); } -> Option AQuick Check:
Conditionally add middleware in development only [OK]
- Trying to disable route after adding middleware
- Using app.get without serving static files
- Reversing environment condition
Ready to prove it?
Pass the test and this pattern moves unknown
