@@ -12,6 +12,15 @@ import { requirePermission } from './middlewares/rbac.middleware';
1212import { staffDataProxy } from './proxy/staff.data.proxy' ;
1313const app = express ( ) ;
1414
15+ const frontendProxy = createProxyMiddleware ( {
16+ target : 'http://localhost:3000' ,
17+ changeOrigin : true ,
18+ } ) ;
19+
20+ const isPageRequest = ( req : any ) => {
21+ return req . method === 'GET' && req . headers . accept ?. includes ( 'text/html' ) ;
22+ } ;
23+
1524app . use (
1625 cors ( {
1726 origin : process . env . ALLOWED_ORIGINS ?. split ( ',' ) ,
@@ -50,9 +59,31 @@ const injectStaffHeaders = (req: any, _res: any, next: any) => {
5059 next ( ) ;
5160} ;
5261
53- app . use ( '/staff' , authenticate , injectStaffHeaders , staffDataProxy ) ;
62+ app . use (
63+ '/staff' ,
64+ ( req , res , next ) => {
65+ if ( isPageRequest ( req ) ) return next ( ) ;
66+ authenticate ( req , res , next ) ;
67+ } ,
68+ injectStaffHeaders ,
69+ ( req , res , next ) => {
70+ if ( isPageRequest ( req ) ) return next ( ) ;
71+ staffDataProxy ( req , res , next ) ;
72+ }
73+ ) ;
5474
55- app . use ( '/admin' , authenticate , injectStaffHeaders , staffDataProxy ) ;
75+ app . use (
76+ '/admin' ,
77+ ( req , res , next ) => {
78+ if ( isPageRequest ( req ) ) return next ( ) ;
79+ authenticate ( req , res , next ) ;
80+ } ,
81+ injectStaffHeaders ,
82+ ( req , res , next ) => {
83+ if ( isPageRequest ( req ) ) return next ( ) ;
84+ staffDataProxy ( req , res , next ) ;
85+ }
86+ ) ;
5687
5788app . use (
5889 '/admissions' ,
@@ -111,4 +142,9 @@ app.use(
111142 } )
112143) ;
113144
145+ // Fallback to frontend for all other requests (pages, static assets, etc.)
146+ app . use ( ( req , res , next ) => {
147+ frontendProxy ( req , res , next ) ;
148+ } ) ;
149+
114150export default app ;
0 commit comments