6

I use the following code and it works

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

But when I try the following I got error,why?

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

        });
07_05_GuyT
  • 2,787
  • 14
  • 43
  • 88

2 Answers2

4

Because httpProxy.createProxyServer uses url.parse, which will take string as argument you can see the documentatin here https://nodejs.org/docs/latest/api/url.html

you can see the proxy server code here https://github.com/nodejitsu/node-http-proxy/blob/master/lib/http-proxy/index.js

refer line number : 64

dayakar
  • 66
  • 1
3

It's because it only works with string. From https://github.com/nodejitsu/node-http-proxy#options:

httpProxy.createProxyServer supports the following options:

target: url string to be parsed with the url module

Community
  • 1
  • 1
Lucian
  • 644
  • 3
  • 13
  • Can you use client-side SSL with this? Seems you need to add cert, key, and/or ca to the target object (which obviously makes it not a string) for SSL, but that makes the url.parse() fail. I am looking for a way to use both autoRewrite and client-side SSL with http-proxy. – Ezra Jun 30 '20 at 17:59