1+ // Updated routes.ts file with improved logging and error handling
12import { Application , Request , Response } from "express" ;
23import proxy from "express-http-proxy" ;
34import { Service } from "./config" ;
@@ -6,14 +7,14 @@ import { Service } from "./config";
67const createMultipartProxy = ( serviceUrl : string ) => {
78 return proxy ( serviceUrl , {
89 proxyReqPathResolver ( req ) {
9- console . log ( `Creating multipart proxy for ${ serviceUrl } ${ req . url } ` ) ;
10+ console . log ( `Multipart proxy: ${ serviceUrl } ${ req . url } ` ) ;
1011 return "/multipart" + req . url ;
1112 } ,
1213 parseReqBody : false ,
1314 reqBodyEncoding : null ,
1415 proxyErrorHandler : function ( err , res , next ) {
1516 console . error ( `Multipart proxy error for ${ serviceUrl } : ${ err . message } ` ) ;
16- res . status ( 500 ) . json ( { error : `Service temporarily unavailable: ${ err . message } ` } ) ;
17+ res . status ( 500 ) . json ( { error : `Service unavailable: ${ err . message } ` } ) ;
1718 }
1819 } ) ;
1920} ;
@@ -22,66 +23,53 @@ const createMultipartProxy = (serviceUrl: string) => {
2223const createRegularProxy = ( serviceUrl : string ) => {
2324 return proxy ( serviceUrl , {
2425 proxyReqPathResolver ( req ) {
25- console . log ( `Proxying request to ${ serviceUrl } ${ req . url } ` ) ;
26+ console . log ( `Regular proxy: ${ serviceUrl } ${ req . url } ` ) ;
2627 return req . url ;
2728 } ,
2829 userResDecorator : function ( proxyRes , proxyResData , userReq , userRes ) {
2930 console . log ( `Response from ${ serviceUrl } ${ userReq . url } : Status ${ proxyRes . statusCode } ` ) ;
30- try {
31- return proxyResData ;
32- } catch ( e ) {
33- console . error ( 'Error in response decorator:' , e ) ;
34- return proxyResData ;
35- }
31+ // Don't modify headers that might cause redirects
32+ return proxyResData ;
3633 } ,
3734 proxyErrorHandler : function ( err , res , next ) {
3835 console . error ( `Proxy error for ${ serviceUrl } : ${ err . message } ` ) ;
39- res . status ( 500 ) . json ( { error : `Service temporarily unavailable: ${ err . message } ` } ) ;
36+ res . status ( 500 ) . json ( { error : `Service unavailable: ${ err . message } ` } ) ;
4037 } ,
4138 timeout : 60000
4239 } ) ;
4340} ;
4441
4542export const routes = ( app : Application ) => {
46- // Add a root health check endpoint
47- app . get ( "/" , ( req : Request , res : Response ) => {
48- res . status ( 200 ) . json ( { status : "API Gateway is running" , services : Object . keys ( Service ) } ) ;
43+ // Add more debugging for incoming requests
44+ app . use ( ( req , res , next ) => {
45+ console . log ( `API Gateway received: ${ req . method } ${ req . url } ` ) ;
46+ console . log ( `Headers: ${ JSON . stringify ( req . headers , null , 2 ) } ` ) ;
47+ next ( ) ;
4948 } ) ;
50-
49+
5150 app . get ( "/health" , ( req : Request , res : Response ) => {
52- res . status ( 200 ) . json ( { status : "API Gateway is running" } ) ;
51+ res . status ( 200 ) . json ( { status : "api-gateway is running" } ) ;
5352 } ) ;
5453
5554 // Use multipart proxy only for file upload routes
5655 app . use ( "/auth/multipart" , createMultipartProxy ( Service . AUTH_SERVICE_URL ) ) ;
5756 // Use regular proxy for other auth routes
5857 app . use ( "/auth" , createRegularProxy ( Service . AUTH_SERVICE_URL ) ) ;
59-
58+
6059 app . use ( "/notification" , createRegularProxy ( Service . NOTIFICATION_SERVICE_URL ) ) ;
61-
60+
6261 // Use multipart proxy only for file upload routes
6362 app . use ( "/course/multipart" , createMultipartProxy ( Service . COURSE_SERVICE_URL ) ) ;
6463 // Use regular proxy for other course routes
6564 app . use ( "/course" , createRegularProxy ( Service . COURSE_SERVICE_URL ) ) ;
66-
65+
6766 app . use ( "/payment" , createRegularProxy ( Service . PAYMENT_SERVICE_URL ) ) ;
68-
67+
6968 app . use ( "/chat" , createRegularProxy ( Service . CHAT_SERVICE_URL ) ) ;
7069
71- // Improve 404 handling with more information
70+ // Catch-all route handler
7271 app . use ( "*" , ( req : Request , res : Response ) => {
7372 console . log ( `404 Not Found: ${ req . method } ${ req . originalUrl } ` ) ;
74- res . status ( 404 ) . json ( {
75- error : "Route not found" ,
76- path : req . originalUrl ,
77- availableRoutes : [
78- "/health" ,
79- "/auth/*" ,
80- "/notification/*" ,
81- "/course/*" ,
82- "/payment/*" ,
83- "/chat/*"
84- ]
85- } ) ;
73+ res . status ( 404 ) . json ( { error : "route not found" } ) ;
8674 } ) ;
8775} ;
0 commit comments