【问题标题】:How to set up http-proxy-middleware on a dynamic route?如何在动态路由上设置 http-proxy-middleware?
【发布时间】:2019-12-20 16:16:34
【问题描述】:

我正在尝试代理一些资产路由,其中​​路径的动态部分来自配置文件。我使用request 库进行了这项工作,但我无法完全使用http-proxy-middleware

这是我使用 request 库时有效的代码

  const assets = express.Router();
  app.use(`/${p.name}/assets`, assets);
  assets.get('*', async (req, res) => {
    return request(`${p.address}/${p.version}/assets${req.path}`).pipe(res);
  });

我用http-proxy-middleware 尝试了几种不同的变体,但这些都不起作用。示例 1:

  app.use(
    `/${p.name}/assets`,
    httpProxy({
      target: `${p.address}`,
      changeOrigin: true,
      pathRewrite: {
        [`^${p.name}`]: `${p.version}`,
      },
    })
  );

示例 2:

  app.use(
    `/${p.name}/assets`,
    httpProxy({
      target: `${p.address}`,
      changeOrigin: true,
      pathRewrite: function(path) {
        return path.replace(p.name, p.version);
      }
    })
  );

我还尝试使用/${p.name}/assets/** 作为app.use 的第一个参数,并且我还尝试将/assets 添加到pathRewrite 对象的键和值的末尾。结果总是一样的:浏览器请求的资产得到 302。

我什至尝试在记录到控制台的 httpProxy 调用之前添加一个中间件函数,以便我知道我的请求正在到达正确的路径:

  app.use(
    `/${p.name}/assets`,
    (req, _res, next) => {
      console.log("I'm an asset proxy, short and stout: ", req.url);
      next();
    },
    httpProxy({
      target: `${p.address}`,
      changeOrigin: true,
      pathRewrite: {
        [`^${p.name}`]: `${p.version}`,
      },
    })
  );

但我从来没有看到过那个输出。可能是一个简单的错误。也许我对app.use 的第一个论点是不对的?感谢您的帮助!

更新

我还尝试了旧方法和新方法的结合。我在/${p.name}/assets 上安装了一个新路由器,在发现onProxyRequest 选项后,我在那里添加了一个函数来记录一些输出。

  const assets = express.Router();
  app.use(`/${p.name}/assets`, assets);
  assets.use(
    '*',
    httpProxy({
      target: p.address,
      changeOrigin: true,
      pathRewrite: {
        [`^${p.name}`]: p.version,
      },
      onProxyReq: (proxyReq, req, res) => {
        console.log("Hello I'm being proxied now! ", proxyReq, req, res);
      },
    })
  );

仍然得到 302,我从未看到 onProxyReq 函数的输出。

【问题讨论】:

    标签: node.js express http-proxy-middleware


    【解决方案1】:

    Documentation 有自定义路由器选项。

    -

    const proxyTable = {
      'integration.localhost:3000': 'http://localhost:8001', // host only
      'staging.localhost:3000': 'http://localhost:8002', // host only
      'localhost:3000/api': 'http://localhost:8003', // host + path
      '/rest': 'http://localhost:8004', // path only
    };
    
    const options = {
      target: 'http://localhost:8000',
      router: proxyTable,
    };
    
    const myProxy = createProxyMiddleware(options);
    
    function customRouter(req) {
      // I dont know what "p" stands for 
      // check the req object to see which property you want to modify
      console.log(req)
      const { hostname } = req
      const hostName = hostname.split('.')[0]
      return `https://${hostName}.sub.domain.com`
    }
    

    然后添加这个选项:

      router: customRouter
    

    【讨论】:

      【解决方案2】:

      你能检查一下你安装的版本吗 - 看起来你正在使用 v0.x.x。 http-proxy-middleware 包的接口。最新版本是 2.X.X,它公开了以下功能

      const { createProxyMiddleware } = require('http-proxy-middleware');
      
      app.use(
        `/${p.name}/assets`,
        createProxyMiddleware({
          target: `${p.address}`,
          changeOrigin: true,
          pathRewrite: {
            [`^${p.name}`]: `${p.version}`,
          },
        })
      );
      

      【讨论】:

        猜你喜欢
        • 2011-12-03
        • 2017-12-08
        • 2022-12-17
        • 1970-01-01
        • 1970-01-01
        • 2019-06-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多