Skip to content

Commit

Permalink
fix(logger-plugin): fix missing target port
Browse files Browse the repository at this point in the history
  • Loading branch information
chimurai committed Apr 20, 2024
1 parent 2217eaf commit 54011eb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/plugins/default/logger-plugin.ts
@@ -1,3 +1,4 @@
import { URL } from 'url';
import { Plugin } from '../../types';
import { getLogger } from '../../logger';

Expand Down Expand Up @@ -26,7 +27,13 @@ export const loggerPlugin: Plugin = (proxyServer, options) => {
// 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
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

0 comments on commit 54011eb

Please sign in to comment.