99import express from "express" ;
1010import cors from "cors" ;
1111import dotenv from "dotenv" ;
12+ import swaggerUi from "swagger-ui-express" ;
1213import {
1314 closeDatabaseConnection ,
1415 connectToDatabase ,
1516 verifyRequirements ,
1617} from "./config/database" ;
1718import { errorHandler } from "./utils/errorHandler" ;
1819import 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(
4446app . use ( express . json ( { limit : "10mb" } ) ) ;
4547app . 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 */
5796app . 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