@@ -6,6 +6,7 @@ import { fileURLToPath } from 'node:url';
66import { Readable } from 'node:stream' ;
77import { createGzip } from 'node:zlib' ;
88import serverHandler from './dist/server/server.js' ;
9+ import { extractUserInfoFromRequest , logRequestInit , logResponse } from './request-logging.js' ;
910
1011const __dirname = fileURLToPath ( new URL ( '.' , import . meta. url ) ) ;
1112const PORT = process . env . PORT || 3030 ;
@@ -68,6 +69,15 @@ const server = createServer(async (req, res) => {
6869 const requestId = req . headers [ 'x-request-id' ] || `ssr-${ Math . random ( ) . toString ( 36 ) . slice ( 2 , 10 ) } ` ;
6970 const requestStart = Date . now ( ) ;
7071
72+ // Parse URL early for logging
73+ const url = new URL ( req . url , `http://${ req . headers . host } ` ) ;
74+ const pathname = url . pathname ;
75+ const method = req . method ;
76+
77+ // Extract user ID and org ID and log request initialization
78+ const { userId, orgId } = await extractUserInfoFromRequest ( req ) ;
79+ logRequestInit ( method , pathname , requestId , userId , orgId ) ;
80+
7181 // Set request timeout
7282 req . setTimeout ( REQUEST_TIMEOUT , ( ) => {
7383 console . error ( `⏱️ Request timeout (${ REQUEST_TIMEOUT } ms): ${ req . method } ${ req . url } [${ requestId } ]` ) ;
@@ -78,8 +88,6 @@ const server = createServer(async (req, res) => {
7888 } ) ;
7989
8090 try {
81- const url = new URL ( req . url , `http://${ req . headers . host } ` ) ;
82- const pathname = url . pathname ;
8391
8492 // Try to serve static files from dist/client first
8593 // Serve: /assets/*, *.js, *.css, *.json, images, fonts, favicons
@@ -99,6 +107,9 @@ const server = createServer(async (req, res) => {
99107 'Cache-Control' : 'public, max-age=31536000, immutable' ,
100108 } ) ;
101109 res . end ( content ) ;
110+ // Log response for static files
111+ const latency = Date . now ( ) - requestStart ;
112+ logResponse ( method , pathname , requestId , latency , 200 ) ;
102113 return ;
103114 } catch ( err ) {
104115 // File not found, fall through to SSR handler
@@ -221,13 +232,20 @@ const server = createServer(async (req, res) => {
221232 } else {
222233 res . end ( ) ;
223234 }
235+
236+ // Log response after sending
237+ const latency = Date . now ( ) - requestStart ;
238+ logResponse ( method , pathname , requestId , latency , res . statusCode ) ;
224239 } catch ( error ) {
225240 console . error ( `Server error [${ requestId } ]:` , error ) ;
226241 if ( ! res . headersSent ) {
227242 res . statusCode = 500 ;
228243 res . setHeader ( 'Content-Type' , 'text/plain' ) ;
229244 res . end ( 'Internal Server Error' ) ;
230245 }
246+ // Log error response
247+ const latency = Date . now ( ) - requestStart ;
248+ logResponse ( method , pathname , requestId , latency , res . statusCode || 500 ) ;
231249 }
232250} ) ;
233251
0 commit comments