Swagger/OpenAPI helps you describe your API clearly so others can understand and use it easily.
Swagger/OpenAPI specification in Express
const swaggerJsdoc = require('swagger-jsdoc'); const swaggerUi = require('swagger-ui-express'); const express = require('express'); const app = express(); 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); app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
The openapi field sets the OpenAPI version.
The apis array points to files where you write comments describing your API.
const options = {
definition: {
openapi: '3.0.0',
info: {
title: 'My API',
version: '1.0.0',
description: 'Simple API example',
},
},
apis: ['./app.js'],
};/**
* @swagger
* /users:
* get:
* summary: Get all users
* responses:
* 200:
* description: List of users
*/This Express app has one route /hello and uses Swagger to document it. You can visit /api-docs to see the interactive 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 with Swagger', }, }, apis: ['./index.js'], }; const swaggerSpec = swaggerJsdoc(options); /** * @swagger * /hello: * get: * summary: Returns a greeting message * responses: * 200: * description: Greeting message * content: * text/plain: * schema: * type: string */ 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'); });
Write your API details in comments using @swagger tags to keep docs updated automatically.
Use swagger-ui-express to serve a friendly web page showing your API docs.
Keep your API description clear and simple for easy understanding.
Swagger/OpenAPI helps describe your API clearly for others.
You add special comments in your code to define endpoints and responses.
Use tools like swagger-ui-express to show interactive API docs in your app.
Practice
Solution
Step 1: Understand Swagger/OpenAPI role
Swagger/OpenAPI is used to describe APIs so others can understand how to use them.Step 2: Identify main purpose in Express
It helps document endpoints, request parameters, and responses clearly.Final Answer:
To clearly describe and document the API endpoints and their behavior -> Option BQuick Check:
Swagger documents APIs clearly [OK]
- Thinking Swagger speeds up the server
- Confusing Swagger with security tools
- Assuming Swagger fixes code bugs automatically
Solution
Step 1: Recall Express middleware usage
Middleware is added with app.use() for routes serving static or UI content.Step 2: Identify swagger-ui-express setup
swaggerUi.serve and swaggerUi.setup() are passed as middleware to app.use() for '/api-docs'.Final Answer:
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument)); -> Option AQuick Check:
Use app.use for middleware setup [OK]
- Using app.get instead of app.use for middleware
- Trying to listen on a route path
- Using app.post for serving docs
{
"paths": {
"/users": {
"post": {
"summary": "Create a new user",
"responses": {
"201": {"description": "User created"}
}
}
}
}
}Solution
Step 1: Read the Swagger path and method
The snippet shows a path '/users' with a 'post' method defined.Step 2: Match HTTP method and path
The method is POST, and the path is '/users', meaning it creates a new user.Final Answer:
POST request to /users -> Option AQuick Check:
Method 'post' means POST request [OK]
- Confusing 'post' with 'get'
- Ignoring the path and focusing on responses
- Assuming default method is GET
import swaggerUi from 'swagger-ui-express';
import swaggerDocument from './swagger.json';
app.use('/docs', swaggerUi.serve, swaggerUi.setup());What is the likely error?
Solution
Step 1: Check swaggerUi.setup() usage
swaggerUi.setup() requires the Swagger document as an argument to display docs.Step 2: Identify missing argument
The code calls swaggerUi.setup() without passing swaggerDocument, so no docs load.Final Answer:
Missing swaggerDocument argument in swaggerUi.setup() -> Option DQuick Check:
Always pass Swagger JSON to swaggerUi.setup() [OK]
- Forgetting to pass swaggerDocument to setup()
- Using app.get instead of app.use for middleware
- Incorrect import syntax causing silent failure
Solution
Step 1: Identify correct parameter location for query
Query parameters use "in": "query" and are usually optional.Step 2: Check parameter details
"parameters": [{"name": "category", "in": "query", "required": false, "schema": {"type": "string"}, "description": "Filter products by category"}] correctly sets name, in, required false, type string, and description.Final Answer:
"parameters": [{"name": "category", "in": "query", "required": false, "schema": {"type": "string"}, "description": "Filter products by category"}] -> Option CQuick Check:
Query params use "in": "query" [OK]
- Using "in": "path" for query parameters
- Setting required true for optional query params
- Using "in": "body" which is invalid in OpenAPI 3.0
Ready to prove it?
Pass the test and this pattern moves unknown
