Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(logger-plugin): fix missing target port #989

Merged
merged 3 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- fix(type): fix RequestHandler return type
- refactor(errors): improve pathFilter error message
- fix(logger-plugin): fix missing target port

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

Expand Down
25 changes: 23 additions & 2 deletions src/plugins/default/logger-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import { URL } from 'url';
import { Plugin } from '../../types';
import { getLogger } from '../../logger';
import type { IncomingMessage } from 'node:http';

type ExpressRequest = {
/** Express req.baseUrl */
baseUrl?: string;
};

type BrowserSyncRequest = {
/** BrowserSync req.originalUrl */
originalUrl?: string;
};

/** Request Types from different server libs */
type FrameworkRequest = IncomingMessage & ExpressRequest & BrowserSyncRequest;

export const loggerPlugin: Plugin = (proxyServer, options) => {
const logger = getLogger(options);
Expand All @@ -22,11 +37,17 @@ export const loggerPlugin: Plugin = (proxyServer, options) => {
* [HPM] GET /users/ -> http://jsonplaceholder.typicode.com/users/ [304]
* ```
*/
proxyServer.on('proxyRes', (proxyRes: any, req: any, res) => {
proxyServer.on('proxyRes', (proxyRes: any, req: FrameworkRequest, res) => {
// BrowserSync uses req.originalUrl
// Next.js doesn't have req.baseUrl
const originalUrl = req.originalUrl ?? `${req.baseUrl || ''}${req.url}`;
const exchange = `[HPM] ${req.method} ${originalUrl} -> ${proxyRes.req.protocol}//${proxyRes.req.host}${proxyRes.req.path} [${proxyRes.statusCode}]`;

// construct targetUrl
const target = new URL(options.target as URL);
target.pathname = proxyRes.req.path;
const targetUrl = target.toString();

const exchange = `[HPM] ${req.method} ${originalUrl} -> ${targetUrl} [${proxyRes.statusCode}]`;
logger.info(exchange);
});

Expand Down
4 changes: 3 additions & 1 deletion test/e2e/http-proxy-middleware.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,9 @@ describe('E2E http-proxy-middleware', () => {

expect(logMessages).not.toBeUndefined();
expect(logMessages.length).toBe(1);
expect(logMessages[0]).toBe('[HPM] GET /api/foo/bar -> http://localhost/api/foo/bar [200]');
expect(logMessages.at(0)).toBe(
`[HPM] GET /api/foo/bar -> http://localhost:${mockTargetServer.port}/api/foo/bar [200]`,
);
});
});
});
Expand Down
Loading