1
+ import { URL } from 'url' ;
1
2
import { Plugin } from '../../types' ;
2
3
import { 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 ;
3
18
4
19
export const loggerPlugin : Plugin = ( proxyServer , options ) => {
5
20
const logger = getLogger ( options ) ;
@@ -22,11 +37,17 @@ export const loggerPlugin: Plugin = (proxyServer, options) => {
22
37
* [HPM] GET /users/ -> http://jsonplaceholder.typicode.com/users/ [304]
23
38
* ```
24
39
*/
25
- proxyServer . on ( 'proxyRes' , ( proxyRes : any , req : any , res ) => {
40
+ proxyServer . on ( 'proxyRes' , ( proxyRes : any , req : FrameworkRequest , res ) => {
26
41
// BrowserSync uses req.originalUrl
27
42
// Next.js doesn't have req.baseUrl
28
43
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 } ]` ;
30
51
logger . info ( exchange ) ;
31
52
} ) ;
32
53
0 commit comments