@@ -6,7 +6,8 @@ import 'express-async-errors';
66import  fs  from  'fs' ; 
77import  helmet  from  'helmet' ; 
88import  path  from  'path' ; 
9- import  {  CORS ,  NODE_ENV ,  SENTRY_DSN ,  VERSION  }  from  './env-vars' ; 
9+ import  {  CORS ,  LOG_REQUEST_INFO ,  NODE_ENV ,  SENTRY_DSN ,  VERSION  }  from  './env-vars' ; 
10+ import  {  logRequestInfo  }  from  './middleware/logRequestInfo' ; 
1011import  internal_router  from  './routes/internal' ; 
1112import  {  ApiError  }  from  './utils/ApiError' ; 
1213
@@ -27,6 +28,11 @@ app.use(helmet());
2728// set CORS origin from env variable 
2829app . use ( cors ( {  origin : CORS  } ) ) ; 
2930
31+ // Request logging middleware for Datadog 
32+ if  ( LOG_REQUEST_INFO )  { 
33+   app . use ( logRequestInfo ) ; 
34+ } 
35+ 
3036// Health-check 
3137app . get ( '/' ,  ( req ,  res )  =>  { 
3238  res . status ( 200 ) . json ( {  message : 'OK'  } ) ; 
@@ -39,36 +45,39 @@ app.get('/health', (req, res) => {
3945// Internal routes 
4046app . use ( '/v0/internal' ,  internal_router ) ; 
4147
42- // Register all our dynamic routes, then regsiter  the error middleware last of all 
48+ // Register all our dynamic routes, then register  the error middleware last of all 
4349registerRoutes ( ) . then ( ( )  =>  { 
4450  // Error-logging middleware 
45-   app . use ( ( err : any ,  req : Request ,  res : Response ,  next : NextFunction )  =>  { 
51+   app . use ( ( error : any ,  req : Request ,  res : Response ,  next : NextFunction )  =>  { 
4652    if  ( NODE_ENV  !==  'test' )  { 
47-       if  ( err . status  >=  500 )  { 
48-         if  ( NODE_ENV  ===  'production' )  console . error ( `[${ new  Date ( ) . toISOString ( ) }  ,  err ) ; 
49-         else  console . log ( `[${ new  Date ( ) . toISOString ( ) }  ,  err ) ; 
53+       if  ( error . status  >=  500 )  { 
54+         if  ( NODE_ENV  ===  'production' )  { 
55+           console . error ( {  time : new  Date ( ) . toISOString ( ) ,  error } ) ; 
56+         }  else  { 
57+           console . log ( {  time : new  Date ( ) . toISOString ( ) ,  error } ) ; 
58+         } 
5059      } 
5160    } 
52-     next ( err ) ; 
61+     next ( error ) ; 
5362  } ) ; 
5463
5564  // Error-handling middleware 
56-   app . use ( ( err : any ,  req : Request ,  res : Response ,  next : NextFunction )  =>  { 
65+   app . use ( ( error : any ,  req : Request ,  res : Response ,  next : NextFunction )  =>  { 
5766    // Check if headers have already been sent 
5867    if  ( res . headersSent )  { 
59-       return  next ( err ) ; 
68+       return  next ( error ) ; 
6069    } 
6170
6271    // Application-specific error handling 
63-     if  ( err  instanceof  ApiError )  { 
64-       res . status ( err . status ) . json ( {  error : {  message : err . message ,  ...( err . meta  ? {  meta : err . meta  }  : { } )  }  } ) ; 
72+     if  ( error  instanceof  ApiError )  { 
73+       res . status ( error . status ) . json ( {  error : {  message : error . message ,  ...( error . meta  ? {  meta : error . meta  }  : { } )  }  } ) ; 
6574    }  else  { 
66-       console . error ( err ) ; 
75+       console . error ( {  error  } ) ; 
6776
6877      // Generic error handling 
69-       res . status ( err . status  ||  500 ) . json ( { 
78+       res . status ( error . status  ||  500 ) . json ( { 
7079        error : { 
71-           message : err . message , 
80+           message : error . message , 
7281        } , 
7382      } ) ; 
7483    } 
@@ -100,7 +109,7 @@ async function registerRoutes() {
100109    if  ( httpMethodIndex  ===  - 1 )  httpMethodIndex  =  segments . indexOf ( 'DELETE' ) ; 
101110
102111    if  ( httpMethodIndex  ===  - 1 )  { 
103-       console . error ( 'File route is malformed. It needs an HTTP method: %s ' ,  file ) ; 
112+       console . error ( {   message :  'File route is malformed. It needs an HTTP method' ,  file  } ) ; 
104113    }  else  { 
105114      const  httpMethod  =  segments [ httpMethodIndex ] . toLowerCase ( )  as  'get'  |  'post'  |  'put'  |  'patch'  |  'delete' ; 
106115      const  routeSegments  =  segments . slice ( 0 ,  httpMethodIndex ) ; 
@@ -112,14 +121,17 @@ async function registerRoutes() {
112121        app [ httpMethod ] ( expressRoute ,  ...callbacks ) ; 
113122        registeredRoutes . push ( httpMethod . toUpperCase ( )  +  ' '  +  expressRoute ) ; 
114123      }  catch  ( err )  { 
115-         console . error ( ` Failed to register route:  ${ expressRoute } ` ,   err ) ; 
124+         console . error ( {   message :  ' Failed to register route' ,   expressRoute,   error :  err   } ) ; 
116125      } 
117126    } 
118127  } 
119128
120129  // Keep around for debugging 
121130  // if (NODE_ENV !== 'production' && NODE_ENV !== 'test') { 
122-   //   console.log(`Dynamically registered routes: ${registeredRoutes.map((route) => `\n  ${route}`).join('')}`); 
131+   //   console.log({ 
132+   //     message: 'Dynamically registered routes', 
133+   //     routes: registeredRoutes.map((route) => `\n  ${route}`).join(''), 
134+   //   }); 
123135  // } 
124136} 
125137
0 commit comments