无法使用http-proxy-middleware(快速服务器)代理url

时间:2019-12-23 17:20:38

标签: express proxy http-proxy-middleware

我正在尝试使用http-proxy-middleware将我的http API重定向到https,因为我的UI在https中,这会导致浏览器端出现混合内容。

我的development.json具有以下网址:

  "urls" : {
    "servicesOneUrl": "http://something.com:4882/my-web/abc/one",
    "servicesTwoUrl": "http:/something.com:4882/my-web/abc/two",
    "servicesThreeUrl": "http://something.com:4882/my-web/three",
    "serviceFourUrl": "http://something.com:4882/my-web/four",
    "serviceFiveUrl": "http://something.com:4882/my-web/abc/five",
    "somethingElseUrl": "https://another.com"
  }

在我的UIServer.js中,函数_createIndexRoute如下:

_createIndexRoute() {
        // proxy middleware options
        var options = {
            target: "http://something.com:4882", // target host
            changeOrigin: true, // needed for virtual hosted sites
            ws: true, // proxy websockets
            pathRewrite: {
                '^/api': '' // remove base path
            },
            onProxyReq: (proxyReq, req, res) => {
                proxyReq.setHeader('X-UserLogin', req.user.username);
                if (req.body) {
                    let bodyData = JSON.stringify(req.body);
                    if (req.get("Content-Type")) {
                        proxyReq.setHeader('Content-Type', req.get("Content-Type"));
                    }
                    proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
                    // stream the content
                    proxyReq.write(bodyData);
                }
            }
        };

        var serverProxy = proxy(options);
        this.app.use('/api', restrictAccess.restrictByCookie(), serverProxy);

        /* Getting the username from request object and passing to the
           client side  so that it is available on the client on page load
         */
        this.app.get('*', restrictAccess.restrictByCookie(), (req, res) => {
            //...
        });
    }

但是当我的UI到达URL http://something.com:4882/my-web/abc/one/someAPI.action时,它不会重定向到相应的https网站。

如何进行这项工作?

0 个答案:

没有答案