Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion server/express/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@
"cors": "^2.8.5",
"dotenv": "^17.2.3",
"express": "^5.1.0",
"mongodb": "^6.20.0"
"mongodb": "^6.20.0",
"swagger-jsdoc": "^6.2.8",
"swagger-ui-express": "^5.0.1"
},
"devDependencies": {
"@types/cors": "^2.8.17",
"@types/express": "^4.17.21",
"@types/jest": "^29.5.14",
"@types/node": "^20.10.5",
"@types/swagger-jsdoc": "^6.0.4",
"@types/swagger-ui-express": "^4.1.8",
"jest": "^29.7.0",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.2",
Expand Down
42 changes: 41 additions & 1 deletion server/express/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
import express from "express";
import cors from "cors";
import dotenv from "dotenv";
import swaggerUi from "swagger-ui-express";
import {
closeDatabaseConnection,
connectToDatabase,
verifyRequirements,
} from "./config/database";
import { errorHandler } from "./utils/errorHandler";
import moviesRouter from "./routes/movies";
import { swaggerSpec } from "./config/swagger";

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

/**
* Swagger API Documentation
* Provides interactive API documentation at /api-docs
*/
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerSpec));

/**
* API Routes
* All movie-related CRUD operations are handled by the movies router
Expand All @@ -53,6 +61,37 @@ app.use("/api/movies", moviesRouter);
/**
* Root Endpoint
* Provides basic information about the API
* @swagger
* /:
* get:
* summary: Get API information
* description: Returns basic information about the API and available endpoints
* tags: [Info]
* responses:
* 200:
* description: API information
* content:
* application/json:
* schema:
* type: object
* properties:
* name:
* type: string
* example: MongoDB Sample MFlix API
* version:
* type: string
* example: 1.0.0
* description:
* type: string
* endpoints:
* type: object
* properties:
* movies:
* type: string
* example: /api/movies
* documentation:
* type: string
* example: /api-docs
*/
app.get("/", (req, res) => {
res.json({
Expand All @@ -62,6 +101,7 @@ app.get("/", (req, res) => {
"Express.js backend demonstrating MongoDB operations with the sample_mflix dataset",
endpoints: {
movies: "/api/movies",
documentation: "/api-docs",
},
});
});
Expand Down Expand Up @@ -94,7 +134,7 @@ async function startServer() {
// Start the Express server
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
console.log(`API documentation available at http://localhost:${PORT}`);
console.log(`API documentation available at http://localhost:${PORT}/api-docs`);
});
} catch (error) {
console.error("Failed to start server:", error);
Expand Down
Loading