diff --git a/src/plugins/default/logger-plugin.ts b/src/plugins/default/logger-plugin.ts index a05b3662..3b996c65 100644 --- a/src/plugins/default/logger-plugin.ts +++ b/src/plugins/default/logger-plugin.ts @@ -44,19 +44,30 @@ export const loggerPlugin: Plugin = (proxyServer, options) => { const originalUrl = req.originalUrl ?? `${req.baseUrl || ''}${req.url}`; // construct targetUrl - const port = getPort(proxyRes.req?.agent?.sockets); + let target: URL; - const obj = { - protocol: proxyRes.req.protocol, - host: proxyRes.req.host, - pathname: proxyRes.req.path, - } as URL; + try { + const port = getPort(proxyRes.req?.agent?.sockets); - const target = new URL(`${obj.protocol}//${obj.host}${obj.pathname}`); + const obj = { + protocol: proxyRes.req.protocol, + host: proxyRes.req.host, + pathname: proxyRes.req.path, + } as URL; - if (port) { - target.port = port; + target = new URL(`${obj.protocol}//${obj.host}${obj.pathname}`); + + if (port) { + target.port = port; + } + // eslint-disable-next-line @typescript-eslint/no-unused-vars + } catch (err) { + // nock issue (https://github.com/chimurai/http-proxy-middleware/issues/1035) + // fallback to old implementation (less correct - without port) + 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}]`;