@@ -121,11 +121,8 @@ const combinedDocs = {
121121
122122app . use ( '/api-docs' , swaggerUi . serve , swaggerUi . setup ( combinedDocs ) ) ;
123123
124- // 3. Proxy Routes with CORS Header Management
125124
126- // Function to process CORS headers on proxied responses
127125const processCorsHeaders = ( headers : any , req : Request ) => {
128- // Remove backend service CORS headers to prevent conflicts
129126 delete headers [ 'access-control-allow-origin' ] ;
130127 delete headers [ 'Access-Control-Allow-Origin' ] ;
131128 delete headers [ 'access-control-allow-credentials' ] ;
@@ -135,7 +132,6 @@ const processCorsHeaders = (headers: any, req: Request) => {
135132 delete headers [ 'access-control-allow-headers' ] ;
136133 delete headers [ 'Access-Control-Allow-Headers' ] ;
137134
138- // Set gateway CORS headers
139135 const origin = req . headers . origin ;
140136 if ( NODE_ENV !== 'production' ) {
141137 headers [ 'Access-Control-Allow-Origin' ] = origin || '*' ;
@@ -152,7 +148,6 @@ const processCorsHeaders = (headers: any, req: Request) => {
152148 return headers ;
153149} ;
154150
155- // Proxy /api/v1/ai to AI Service
156151app . use ( '/api/v1/ai' , proxy ( API_AI_URL , {
157152 proxyReqPathResolver : ( req ) => req . originalUrl ,
158153 preserveHostHdr : true ,
@@ -169,27 +164,21 @@ app.use('/api/v1/ai', proxy(API_AI_URL, {
169164 userResHeaderDecorator : ( headers , req ) => processCorsHeaders ( headers , req as Request )
170165} ) ) ;
171166
172- // Catch-all for /api/v1 to proxy to User Service
173167app . use ( '/api/v1' , proxy ( API_USER_URL , {
174168 proxyReqPathResolver : ( req ) => req . originalUrl ,
175169 preserveHostHdr : true ,
176- // Preserve headers
177170 proxyReqOptDecorator : ( proxyReqOpts , srcReq ) => {
178171 proxyReqOpts . headers = {
179172 ...proxyReqOpts . headers ,
180173 'Origin' : srcReq . headers . origin || 'http://localhost:3002' ,
181174 } ;
182175 return proxyReqOpts ;
183176 } ,
184- // Ensure the body is correctly forwarded after being parsed by express.json()
185177 proxyReqBodyDecorator : ( bodyContent , srcReq ) => {
186178 return srcReq . body && Object . keys ( srcReq . body ) . length ? JSON . stringify ( srcReq . body ) : bodyContent ;
187179 } ,
188- // Process CORS headers on response
189180 userResHeaderDecorator : ( headers , req ) => processCorsHeaders ( headers , req as Request )
190181} ) ) ;
191-
192- // Health Check
193182app . get ( '/' , ( req : Request , res : Response ) => {
194183 res . json ( { success : true , message : 'API Gateway is running' } ) ;
195184} ) ;
0 commit comments