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

options.on.proxyRes NOT work at req.headers.accept = text/event-stream on Koa2

Open axolo opened this issue 7 months ago • 3 comments

Checks

Describe the bug (be clear and concise)

koa-router

const { createProxyMiddleware } = require('http-proxy-middleware');
const convert = require('koa-connect');

module.exports = app => {
  const { router, config } = app;

  const proxy = createProxyMiddleware({
    target: config.dify.baseUrl,
    changeOrigin: true,
    ws: true,
    timeout: 0,
    pathRewrite: { '^/dify': '' },
    headers: {
      Authorization: 'Bearer ' + config.dify.apiKey,
    },
    on: {
      proxyReq: (proxyReq, req) => {
        console.log('proxyReq:', req.headers.accept); // text/event-stream
      },
      proxyRes: (proxyRes, req) => {
        // ONLY req.headers.accept = text/event-stream NOT work
        console.log('proxyRes:', req.headers.accept);
        if (req.headers.accept === 'text/event-stream') {
          proxyRes.headers['Content-Type'] = 'text/event-stream; charset=utf-8';
          proxyRes.headers['Cache-Control'] = 'no-cache';
          proxyRes.headers.Connection = 'keep-alive';
        }
      },
    },
  });

  router.all(/^\/dify/, async (ctx, next) => {
    await convert(proxy)(ctx, next);
  });
};

Step-by-step reproduction instructions

1. client use @microsoft/fetch-event-source fetchEventSource POST /chat-messages
2. on.proxyReq console.log proxyReq: text/event-stream
3. on.proxyRes console.log not work and client fetch 504 timeout error
4. client use fetch GET /info, work good, on.proxyReq and on.proxyRes console.log accept application/json

Expected behavior (be clear and concise)

client request accept text/event-stream by EventSource and server streaming output event message from sse

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

[email protected] D:\Projects\reader-api
└── [email protected]

What http-proxy-middleware configuration are you using?

see koa-router

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

Windows 11 10.0.26100 x64
Node: 22.14.0
Edge: Chromium (133.0.3065.69)

Additional context (optional)

No response

axolo avatar Apr 16 '25 02:04 axolo

Can you provide a working reproduction of this issue so it can be investigated?

chimurai avatar Apr 17 '25 19:04 chimurai

Client

import { fetchEventSource } from '@microsoft/fetch-event-source'

try {
  const ctrl = new AbortController()
  const loading = 'Loading...'
  const answer = ''

  fetchEventSource('/chat-messages', { // url proxy by http-proxy-middleware
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      Authorization: 'Bearer {API_KEY}'
    },
    body: JSON.stringify({
    	user: 'fym',
    	query: 'Hello',
    	response_mode: 'streaming',
    	inputs: {}
    }),
    signal: ctrl.signal,
    onopen: e => {
      console.log('sse open', e)
    },
    onclose: e => {
      console.log('sse close', e)
      loading = 'closed'
    },
    onerror: e => {
      console.log('sse error', e)
      loading = e.message
      ctrl.abort()
    },
    onmessage: e => {
      try {
        const data = JSON.parse(e.data)
        loading = data.event
        switch (data.event) {
          default:
            break
          case 'message':
            answer += data.answer
            break
          case 'end':
            loading = ''
            break
          case 'error':
            loading = data.error
            ctrl.abort()
            break
        }
      } catch (error) {
        console.log('sse parse error', error)
        // Dify response stream, may parse json error
        // Please ignore, do NOT ctrl.abort()
      }
    } catch (e) {
      console.log('sse catch', e)
      loading = e.message
      ctrl.abort()
    }
  }
}

axolo avatar Apr 18 '25 01:04 axolo

Thanks. Without a working reproduction of the issue (client and server) I'm not able to help.

Would be nice if you can provide a project on github with the reproduction of the issue

chimurai avatar Apr 18 '25 18:04 chimurai