/** * EduRecommender — Express server entry point. * * Serves the React frontend (static build) and the REST API. * Designed for Docker-based deployment on HuggingFace Spaces. */ // MUST be the first import — loads .env before any other module reads process.env import "./env.js"; import express from "express"; import cors from "cors"; import helmet from "helmet"; import compression from "compression"; import { fileURLToPath } from "url"; import { dirname, join } from "path"; import { apiRouter } from "./routes.js"; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); const PORT = process.env.PORT || 7860; const app = express(); // --------------------------------------------------------------------------- // Middleware // --------------------------------------------------------------------------- app.use(helmet({ contentSecurityPolicy: false, frameguard: false })); app.use(compression()); app.use(cors()); app.use(express.json()); // --------------------------------------------------------------------------- // API routes // --------------------------------------------------------------------------- app.use("/api", apiRouter); // --------------------------------------------------------------------------- // Serve React build (production) // --------------------------------------------------------------------------- const clientBuild = join(__dirname, "../../client/dist"); app.use(express.static(clientBuild)); app.get("*", (_req, res) => { res.sendFile(join(clientBuild, "index.html")); }); // --------------------------------------------------------------------------- // Start // --------------------------------------------------------------------------- app.listen(PORT, "0.0.0.0", () => { console.log(`[EduRecommender] Server running on http://0.0.0.0:${PORT}`); console.log(`[EduRecommender] HF_TOKEN: ${process.env.HF_TOKEN ? "configured" : "not set"}`); });