【问题标题】:Dynamic Port in http-proxy-middleware route/path-rewritehttp-proxy-middleware route/path-rewrite 中的动态端口
【发布时间】:2018-01-22 06:46:09
【问题描述】:

是否可以使用 http-proxy-middleware 执行以下路由/路径重写?

'/sec/port/xxx/yyy' => 目标:'https://someotherSite.com:port/xxx/yyy'

根据初始地址,端口是动态的,即

/sec/1234/xxx/yyy => https://someotherSite.com:1234/xxx/yyy

我正在使用快速服务器。

【问题讨论】:

    标签: node.js http-proxy-middleware


    【解决方案1】:

    您需要重写路径以便删除 /api/PORT,这可以在 pathRewrite 函数中完成。 newPort 是从 req 的原始 url 拆分的第二个索引得到的端口值。

    路由器从请求参数中获取所需的端口号,简单地连接起来作为返回值,动态改变目标为localhost:PORTNUM。

    希望这会有所帮助。

    proxyTable: {
    '/api': {
        target: 'http://localhost',
        changeOrigin: true,
        pathRewrite:
            function(path,req) {
                //Get the port number
                var newPort = req.originalUrl.split('/')[2];
                //Return the path with the api and portname removed
                return path.replace('/api/'+newPort,'');
            },
        router: function(req) {
            var newPort = req.originalUrl.split('/')[2];
            //Dynamically update the port number to the target in router
            return 'http://localhost:'+newPort;
        }
    }
    

    【讨论】:

    • 这是第一次工作,但在重定向时,它需要下一个 / 作为端口
    猜你喜欢
    • 2019-05-23
    • 1970-01-01
    • 2021-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-26
    • 2017-03-05
    相关资源
    最近更新 更多