@@ -6,35 +6,60 @@ 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
1517if ( process . env . TRUST_PROXY === "true" ) {
1618 app . set ( "trust proxy" , true ) ;
1719}
20+ // Required for __dirname in ES modules / TS
21+ // (TS compiles to CJS so this works fine)
22+ const __dirnameLocal = path . resolve ( ) ;
23+
1824app . use ( requestLogger ) ;
1925app . use ( express . json ( ) ) ;
20- app . use ( cookieParser ( ) ) ; // <-- Add this middleware HERE
26+ app . use ( cookieParser ( ) ) ;
27+
28+ // CORS
2129app . use (
2230 cors ( {
2331 origin : process . env . FRONTEND_URL || "http://localhost:5174" ,
2432 credentials : true ,
2533 methods : [ "GET" , "POST" , "PUT" , "DELETE" , "OPTIONS" ] ,
2634 } )
2735) ;
28- //initialize passport
36+
37+ // Passport
2938app . use ( passport . initialize ( ) ) ;
30- // Routes
39+
40+ // Uploaded avatars: stored in /uploads/avatars
41+ app . use (
42+ "/avatars" ,
43+ express . static ( path . join ( __dirnameLocal , "uploads" , "avatars" ) )
44+ ) ;
45+
46+ // Default avatars: stored in /public/default-avatars
47+ app . use (
48+ "/default-avatars" ,
49+ express . static ( path . join ( __dirnameLocal , "public" , "default-avatars" ) )
50+ ) ;
51+
52+ // -------------------------
53+ // API Routes
54+ // -------------------------
3155app . use ( "/api/auth" , authRoutes ) ;
3256app . use ( "/api/health" , healthRoutes ) ;
3357app . use ( "/api/rooms" , roomRoutes ) ;
3458
3559// 404 handler
3660app . use ( notFoundHandler ) ;
37- // Error Handler
61+
62+ // Error handler
3863app . use ( errorHandler ) ;
3964
4065export default app ;
0 commit comments