目标的Http代理错误

sho*_*a T 6 node.js node-http-proxy

我使用以下代码,它的工作原理

          proxy.web(req, res, {
                changeOrigin: true,
                target: 'http://' + hostname + ':' + port,
                ws: true
            });
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试以下时,我得到了错误,为什么?

proxy.web(req, res, {
                target: {
                    host: 'http://' + hostname,
                    port: port
                },

        });
Run Code Online (Sandbox Code Playgroud)

小智 3

这是因为它只适用于字符串。来自https://github.com/nodejitsu/node-http-proxy#options

httpProxy.createProxyServer支持以下选项:

target:要使用 url 模块解析的 url 字符串