alanhe421 / coding-note

note
3 stars 1 forks source link

node-http-proxy #443

Open alanhe421 opened 1 year ago

alanhe421 commented 1 year ago

express.json() (body-parser) breaks http-proxy-middleware POST request

if (!req.body || !Object.keys(req.body).length) {
    return;
  }

  const contentType = proxyReq.getHeader('Content-Type');
  const writeBody = (bodyData: string) => {
    proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
    proxyReq.write(bodyData);
  };

  if (contentType.toString().includes('application/json')) {
    writeBody(JSON.stringify(req.body));
  }

  if (contentType.toString().includes('application/x-www-form-urlencoded')) {
    writeBody(querystring.stringify(req.body));
  }
alanhe421 commented 1 year ago

Invalid frame header

image

alanhe421 commented 1 year ago

HPE_CB_HEADERS_COMPLETE

options比如携带了koa的context,可能会触发该错误

proxyServer.on('proxyReq', (proxyReq, req, res, options) => {

});

alanhe421 commented 1 year ago

websocket代理

这里的socket是TCP Socket并非websocket.

proxy.ws(req, socket, head);