Skip to content

Commit dff7906

Browse files
authored
Merge pull request #25 from dacharyc/express-add-docs
Express: Add Swagger API docs
2 parents d7c83d3 + aeca8fc commit dff7906

File tree

5 files changed

+1112
-57
lines changed

5 files changed

+1112
-57
lines changed

server/express/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,17 @@
2121
"cors": "^2.8.5",
2222
"dotenv": "^17.2.3",
2323
"express": "^5.1.0",
24-
"mongodb": "^6.20.0"
24+
"mongodb": "^6.20.0",
25+
"swagger-jsdoc": "^6.2.8",
26+
"swagger-ui-express": "^5.0.1"
2527
},
2628
"devDependencies": {
2729
"@types/cors": "^2.8.17",
2830
"@types/express": "^4.17.21",
2931
"@types/jest": "^29.5.14",
3032
"@types/node": "^20.10.5",
33+
"@types/swagger-jsdoc": "^6.0.4",
34+
"@types/swagger-ui-express": "^4.1.8",
3135
"jest": "^29.7.0",
3236
"ts-jest": "^29.1.1",
3337
"ts-node": "^10.9.2",

server/express/src/app.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99
import express from "express";
1010
import cors from "cors";
1111
import dotenv from "dotenv";
12+
import swaggerUi from "swagger-ui-express";
1213
import {
1314
closeDatabaseConnection,
1415
connectToDatabase,
1516
verifyRequirements,
1617
} from "./config/database";
1718
import { errorHandler } from "./utils/errorHandler";
1819
import moviesRouter from "./routes/movies";
20+
import { swaggerSpec } from "./config/swagger";
1921

2022
// Load environment variables from .env file
2123
// This must be called before any other imports that use environment variables
@@ -44,6 +46,12 @@ app.use(
4446
app.use(express.json({ limit: "10mb" }));
4547
app.use(express.urlencoded({ extended: true, limit: "10mb" }));
4648

49+
/**
50+
* Swagger API Documentation
51+
* Provides interactive API documentation at /api-docs
52+
*/
53+
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerSpec));
54+
4755
/**
4856
* API Routes
4957
* All movie-related CRUD operations are handled by the movies router
@@ -53,6 +61,37 @@ app.use("/api/movies", moviesRouter);
5361
/**
5462
* Root Endpoint
5563
* Provides basic information about the API
64+
* @swagger
65+
* /:
66+
* get:
67+
* summary: Get API information
68+
* description: Returns basic information about the API and available endpoints
69+
* tags: [Info]
70+
* responses:
71+
* 200:
72+
* description: API information
73+
* content:
74+
* application/json:
75+
* schema:
76+
* type: object
77+
* properties:
78+
* name:
79+
* type: string
80+
* example: MongoDB Sample MFlix API
81+
* version:
82+
* type: string
83+
* example: 1.0.0
84+
* description:
85+
* type: string
86+
* endpoints:
87+
* type: object
88+
* properties:
89+
* movies:
90+
* type: string
91+
* example: /api/movies
92+
* documentation:
93+
* type: string
94+
* example: /api-docs
5695
*/
5796
app.get("/", (req, res) => {
5897
res.json({
@@ -62,6 +101,7 @@ app.get("/", (req, res) => {
62101
"Express.js backend demonstrating MongoDB operations with the sample_mflix dataset",
63102
endpoints: {
64103
movies: "/api/movies",
104+
documentation: "/api-docs",
65105
},
66106
});
67107
});
@@ -94,7 +134,7 @@ async function startServer() {
94134
// Start the Express server
95135
app.listen(PORT, () => {
96136
console.log(`Server running on port ${PORT}`);
97-
console.log(`API documentation available at http://localhost:${PORT}`);
137+
console.log(`API documentation available at http://localhost:${PORT}/api-docs`);
98138
});
99139
} catch (error) {
100140
console.error("Failed to start server:", error);

0 commit comments

Comments
 (0)