@@ -25,13 +25,6 @@ if (NODE_ENV === 'production') {
2525 app . set ( 'trust proxy' , 1 ) ;
2626}
2727
28- // Define routes BEFORE global middleware like cors and parsers
29- // This allows routes to have their own specific middleware (like the manual OPTIONS handler)
30- app . use ( '/auth' , authRoutes ) ;
31- app . use ( '/users' , userRoutes ) ;
32- app . use ( '/messages' , messageRoutes ) ;
33- app . use ( '/' , indexRoutes ) ;
34-
3528app . use ( logger ( 'dev' ) ) ;
3629app . use ( express . json ( ) ) ;
3730app . use ( express . urlencoded ( { extended : false } ) ) ;
@@ -57,18 +50,24 @@ app.use(passport.initialize());
5750app . use ( passport . session ( ) ) ;
5851app . use ( passport . authenticate ( 'session' ) ) ;
5952
60- // catch 404 and forward to the error handler
61- app . use ( function ( req , res , next ) {
62- next ( createError ( 404 ) ) ;
63- } ) ;
64-
6553// Apply CORS as a final step before the error handler
6654app . use ( cors ( {
6755 origin : ORIGIN ,
6856 credentials : true ,
6957 optionsSuccessStatus : 204
7058} ) ) ;
7159
60+ // Define routes AFTER session and passport middleware
61+ app . use ( '/auth' , authRoutes ) ;
62+ app . use ( '/users' , userRoutes ) ;
63+ app . use ( '/messages' , messageRoutes ) ;
64+ app . use ( '/' , indexRoutes ) ;
65+
66+ // catch 404 and forward to the error handler
67+ app . use ( function ( req , res , next ) {
68+ next ( createError ( 404 ) ) ;
69+ } ) ;
70+
7271// error handler
7372app . use ( function ( err , req , res ) {
7473 // set locals, only providing error in development
0 commit comments