Skip to content

Commit e644a22

Browse files
authored
fix(logger-plugin): fix missing target port (#989)
* fix(logger-plugin): fix missing target port * refactor(logger-plugin): improve req type * docs(CHANGELOG.md): update changelog
1 parent bffa0a6 commit e644a22

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- fix(type): fix RequestHandler return type
66
- refactor(errors): improve pathFilter error message
7+
- fix(logger-plugin): fix missing target port
78

89
## [v3.0.0](https://github.com/chimurai/http-proxy-middleware/releases/tag/v3.0.0)
910

src/plugins/default/logger-plugin.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
1+
import { URL } from 'url';
12
import { Plugin } from '../../types';
23
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;
318

419
export 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

test/e2e/http-proxy-middleware.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,9 @@ describe('E2E http-proxy-middleware', () => {
459459

460460
expect(logMessages).not.toBeUndefined();
461461
expect(logMessages.length).toBe(1);
462-
expect(logMessages[0]).toBe('[HPM] GET /api/foo/bar -> http://localhost/api/foo/bar [200]');
462+
expect(logMessages.at(0)).toBe(
463+
`[HPM] GET /api/foo/bar -> http://localhost:${mockTargetServer.port}/api/foo/bar [200]`,
464+
);
463465
});
464466
});
465467
});

0 commit comments

Comments
 (0)