Skip to content
Open
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
42 changes: 30 additions & 12 deletions backend/app.js
Original file line number Diff line number Diff line change
@@ -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);