chimurai / http-proxy-middleware

:zap: The one-liner node.js http-proxy middleware for connect, express, next.js and more
MIT License
10.68k stars 826 forks source link

websocket path is wrong after createProxyMiddleware #858

Open alanhe421 opened 1 year ago

alanhe421 commented 1 year ago

Checks

Describe the bug (be clear and concise)

const jsonPlaceholderProxy = createProxyMiddleware({
  target: 'http://127.0.0.1:36001',
  ws: true, changeOrigin: true,
  agent: proxyServer.agent,
  logger: console,
  secure: false,
  pathRewrite: function (path, req) {
    let newPath = path.replace('/tty/9df90607382c056d8312cb77e368aa03', '');
    // if (newPath.indexOf('.websocket') > -1) {
    //   newPath = newPath.replace('/.websocket', '');
    // }
    console.log('newPath', newPath);
    return newPath
  }

});

for ws connection.

the correct path is /stable-129500ee4c8ab7263461ffe327268ba56b9f210d?reconnectionToken=2ba87094-7ef8-4dd6-93dc-70f36a7d9f74&reconnection=false&skipWebSocketFrames=false

but output /stable-129500ee4c8ab7263461ffe327268ba56b9f210d/.websocket?reconnectionToken=2ba87094-7ef8-4dd6-93dc-70f36a7d9f74&reconnection=false&skipWebSocketFrames=false the page i visit is ws://127.0.0.1:8000/tty/9df90607382c056d8312cb77e368aa03/stable-129500ee4c8ab7263461ffe327268ba56b9f210d?reconnectionToken=d1dc2f35-4826-4920-a576-1204ce54b951&reconnection=false&skipWebSocketFrames=false

/.websocket is wrong??

Step-by-step reproduction instructions

just like the code snippet?

Expected behavior (be clear and concise)

path is correct.

How is http-proxy-middleware used in your project?

express-demo@0.0.1 /Users/alanhe/GitHub/express-demo
└── http-proxy-middleware@2.0.6

What http-proxy-middleware configuration are you using?

const jsonPlaceholderProxy = createProxyMiddleware({
  target: 'http://127.0.0.1:36001',
  ws: true, changeOrigin: true,
  agent: proxyServer.agent,
  logger: console,
  secure: false,
  pathRewrite: function (path, req) {
    let newPath = path.replace('/tty/9df90607382c056d8312cb77e368aa03', '');
    // if (newPath.indexOf('.websocket') > -1) {
    //   newPath = newPath.replace('/.websocket', '');
    // }
    console.log('newPath', newPath);
    return newPath
  }
});

What OS/version and node/version are you seeing the problem?

1. Big Sur 11.7
2. node.js@14.18.0

Additional context (optional)

No response