@@ -6,32 +6,57 @@ import { errorHandler, notFoundHandler } from "./middleware/errorHandler.js";
66import { requestLogger } from "./middleware/requestLogger.js" ;
77import roomRoutes from "./routes/roomRoutes.js" ;
88import passport from "passport" ;
9- import "./utils/passport.js"
10- import cookieParser from ' cookie-parser' ;
9+ import "./utils/passport.js" ;
10+ import cookieParser from " cookie-parser" ;
1111import cors from "cors" ;
12+ import path from "path" ;
13+
1214dotenv . config ( ) ;
1315const app = express ( ) ;
1416
17+ // Required for __dirname in ES modules / TS
18+ // (TS compiles to CJS so this works fine)
19+ const __dirnameLocal = path . resolve ( ) ;
20+
1521app . use ( requestLogger ) ;
1622app . use ( express . json ( ) ) ;
17- app . use ( cookieParser ( ) ) ; // <-- Add this middleware HERE
23+ app . use ( cookieParser ( ) ) ;
24+
25+ // CORS
1826app . use (
1927 cors ( {
2028 origin : process . env . FRONTEND_URL || "http://localhost:5174" ,
2129 credentials : true ,
2230 methods : [ "GET" , "POST" , "PUT" , "DELETE" , "OPTIONS" ] ,
2331 } )
2432) ;
25- //initialize passport
33+
34+ // Passport
2635app . use ( passport . initialize ( ) ) ;
27- // Routes
36+
37+ // Uploaded avatars: stored in /uploads/avatars
38+ app . use (
39+ "/avatars" ,
40+ express . static ( path . join ( __dirnameLocal , "uploads" , "avatars" ) )
41+ ) ;
42+
43+ // Default avatars: stored in /public/default-avatars
44+ app . use (
45+ "/default-avatars" ,
46+ express . static ( path . join ( __dirnameLocal , "public" , "default-avatars" ) )
47+ ) ;
48+
49+ // -------------------------
50+ // API Routes
51+ // -------------------------
2852app . use ( "/api/auth" , authRoutes ) ;
2953app . use ( "/api/health" , healthRoutes ) ;
3054app . use ( "/api/rooms" , roomRoutes ) ;
3155
3256// 404 handler
3357app . use ( notFoundHandler ) ;
34- // Error Handler
58+
59+ // Error handler
3560app . use ( errorHandler ) ;
3661
3762export default app ;
0 commit comments