1+ import { URL } from 'url' ;
12import { Plugin } from '../../types' ;
23import { getLogger } from '../../logger' ;
4+ import type { IncomingMessage } from 'node:http' ;
5+
6+ type ExpressRequest = {
7+ /** Express req.baseUrl */
8+ baseUrl ?: string ;
9+ } ;
10+
11+ type BrowserSyncRequest = {
12+ /** BrowserSync req.originalUrl */
13+ originalUrl ?: string ;
14+ } ;
15+
16+ /** Request Types from different server libs */
17+ type FrameworkRequest = IncomingMessage & ExpressRequest & BrowserSyncRequest ;
318
419export const loggerPlugin : Plugin = ( proxyServer , options ) => {
520 const logger = getLogger ( options ) ;
@@ -22,11 +37,17 @@ export const loggerPlugin: Plugin = (proxyServer, options) => {
2237 * [HPM] GET /users/ -> http://jsonplaceholder.typicode.com/users/ [304]
2338 * ```
2439 */
25- proxyServer . on ( 'proxyRes' , ( proxyRes : any , req : any , res ) => {
40+ proxyServer . on ( 'proxyRes' , ( proxyRes : any , req : FrameworkRequest , res ) => {
2641 // BrowserSync uses req.originalUrl
2742 // Next.js doesn't have req.baseUrl
2843 const originalUrl = req . originalUrl ?? `${ req . baseUrl || '' } ${ req . url } ` ;
29- const exchange = `[HPM] ${ req . method } ${ originalUrl } -> ${ proxyRes . req . protocol } //${ proxyRes . req . host } ${ proxyRes . req . path } [${ proxyRes . statusCode } ]` ;
44+
45+ // construct targetUrl
46+ const target = new URL ( options . target as URL ) ;
47+ target . pathname = proxyRes . req . path ;
48+ const targetUrl = target . toString ( ) ;
49+
50+ const exchange = `[HPM] ${ req . method } ${ originalUrl } -> ${ targetUrl } [${ proxyRes . statusCode } ]` ;
3051 logger . info ( exchange ) ;
3152 } ) ;
3253
0 commit comments