web-infra-dev / rsbuild

The Rspack-based build tool. It's fast, out-of-the-box and extensible.
https://rsbuild.dev/
MIT License
1.78k stars 139 forks source link

[Bug]: `onProxyReq: function(proxyReq, req, res)` res.redirect is not a function #3924

Open ultravires opened 19 hours ago

ultravires commented 19 hours ago

Version

System: OS: Windows 11 10.0.22631 CPU: (32) x64 AMD Ryzen 9 7945HX with Radeon Graphics Memory: 2.00 GB / 15.69 GB Browsers: Edge: Chromium (127.0.2651.105) Internet Explorer: 11.0.22621.3527 npmPackages: @rsbuild/core: 1.0.18 => 1.0.18 @rsbuild/plugin-vue2: ^1.0.1 => 1.0.1

Details

      '/a.path/': {
        target: process.env.VUE_APP_BASE_URL,
        changeOrigin: true,
        onProxyReq: function (proxyReq, req, res) {
          res.redirect(`https://some.xx.com${req._parsedUrl.path}`);
          return true;
        },
      },
TypeError: res.redirect is not a function
    at ProxyServer.onProxyReq (D:/Workspaces/WebProjects/leadal/cis-workspace/rsbuild.config.mjs:179:15)
    at ProxyServer.emit (D:\Workspaces\WebProjects\leadal\cis-workspace\node_modules\.pnpm\@rsbuild+core@1.0.18\node_modules\@rsbuild\core\compiled\http-proxy-middleware\index.js:705:35)
    at ClientRequest.<anonymous> (D:\Workspaces\WebProjects\leadal\cis-workspace\node_modules\.pnpm\@rsbuild+core@1.0.18\node_modules\@rsbuild\core\compiled\http-proxy-middleware\index.js:2709:22)
    at ClientRequest.emit (node:events:519:28)
    at tickOnSocket (node:_http_client:843:7)
    at onSocketNT (node:_http_client:906:5)
    at process.processTicksAndRejections (node:internal/process/task_queues:83:21)

Node.js v20.18.0
 ELIFECYCLE  Command failed with exit code 1.

Reproduce link

https://codesandbox.io/p/github/rspack-contrib/rsbuild-codesandbox-example/main

Reproduce Steps

  1. pnpm i pnpm dev
  2. check the logs
9aoy commented 17 hours ago

The res type of http-proxy-middleware refers to express and res.redirect is express only, but express is not actually used in rsbuild. This is a known issue of http-proxy-middleware v2 and is fixed in v3. Since http-proxy-middleware v2 to v3 has some breaking changes for users, we will think about how to handle it in rsbuild.

https://github.com/chimurai/http-proxy-middleware/pull/730