http-proxy-middleware icon indicating copy to clipboard operation
http-proxy-middleware copied to clipboard

websocket path is wrong after createProxyMiddleware

Open alanhe421 opened this issue 2 years ago • 0 comments

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?

[email protected] /Users/alanhe/GitHub/express-demo
└── [email protected]

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. [email protected]

Additional context (optional)

No response

alanhe421 avatar Dec 04 '22 15:12 alanhe421