Skip to content

Commit d303d25

Browse files
author
jurgengjoncari
committed
reorder routes and middlewares
1 parent 50735cd commit d303d25

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

app.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
3528
app.use(logger('dev'));
3629
app.use(express.json());
3730
app.use(express.urlencoded({ extended: false }));
@@ -57,18 +50,24 @@ app.use(passport.initialize());
5750
app.use(passport.session());
5851
app.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
6654
app.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
7372
app.use(function(err, req, res) {
7473
// set locals, only providing error in development

0 commit comments

Comments
 (0)