From 5cc3124e76faad3a9e8f8826ce0840c4261e17e4 Mon Sep 17 00:00:00 2001 From: sultan arif <53237271+sultanarif-p@users.noreply.github.com> Date: Sat, 11 Oct 2025 00:11:51 +0300 Subject: [PATCH] Update app.js Comments added --- backend/app.js | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/backend/app.js b/backend/app.js index 9728801..d5ab9b6 100644 --- a/backend/app.js +++ b/backend/app.js @@ -1,34 +1,52 @@ -import express from "express"; -import cors from "cors"; -import mongoose from "mongoose"; -import pino from "pino"; +// Import required modules +import express from "express"; // Express framework for building the API +import cors from "cors"; // Middleware to handle Cross-Origin Resource Sharing +import mongoose from "mongoose"; // MongoDB ODM for database operations +import pino from "pino"; // Logger for structured logging +// Import controllers for different routes import HealthController from "./controllers/health/health-controller.js"; - import FileUploadController from "./controllers/file-upload/file-upload-controller.js"; -import file_utils from "./utils/file-utils.js"; - +import file_utils from "./utils/file-utils.js"; // Utility for file handling import ResumeController from "./controllers/resume/resume-controller.js"; +// MongoDB connection string +// If environment variable DB_CONNECTION_STRING exists, use it; otherwise, use local MongoDB const CONNECTION_STRING = process.env.DB_CONNECTION_STRING || "mongodb://localhost:27017/TemplateBuddy"; + +// Connect to MongoDB mongoose.connect(CONNECTION_STRING); + +// Initialize logger with different levels depending on environment const logger = pino({ level: process.env.NODE_ENV === "production" ? "info" : "debug", }); +// CORS options configuration +// origin: "*" allows all domains to access the API +// credentials: true allows cookies and authentication headers +// optionSuccessStatus: 200 ensures preflight requests succeed const cors_options = { origin: "*", credentials: true, optionSuccessStatus: 200, }; + +// Initialize Express app const app = express(); -app.use(cors(cors_options)); -app.use(express.json()); + +// Apply middleware +app.use(cors(cors_options)); // Enable CORS with the defined options +app.use(express.json()); // Parse incoming JSON requests + +// Serve static files (like images) from the TEMPLATE_BUDDY_DIR app.use("/images", express.static(file_utils.TEMPLATE_BUDDY_DIR)); -HealthController(app, logger); -FileUploadController(app, logger); -ResumeController(app, logger); +// Setup API routes using controllers +HealthController(app, logger); // Health check endpoints +FileUploadController(app, logger); // File upload endpoints +ResumeController(app, logger); // Resume management endpoints +// Start the server on the specified PORT environment variable or default to 4000 app.listen(process.env.PORT || 4000);