Skip to content

Cannot read property 'protocol' of undefined #499

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
thejhh opened this issue Oct 14, 2013 · 20 comments
Closed

Cannot read property 'protocol' of undefined #499

thejhh opened this issue Oct 14, 2013 · 20 comments

Comments

@thejhh
Copy link

thejhh commented Oct 14, 2013

README has this example for custom application logic (for caronte tree):

    var http = require('http'),
        httpProxy = require('http-proxy');

    //
    // Create a proxy server with custom application logic
    //
    var proxy = httpProxy.createProxyServer({});

    var server = require('http').createServer(function(req, res) {
        proxy.web(req, res, { target: 'http://127.0.0.1:8001' });
    });

    console.log("listening on port 9000")
    server.listen(9000);

I'm getting these errors:

$ node test.js 
listening on port 9000

/opt/nor-web-proxy/node_modules/http-proxy/lib/http-proxy/passes/web-incoming.js:103
    var proxyReq = (server.options.target.protocol === 'https:' ? https : http
                                         ^
TypeError: Cannot read property 'protocol' of undefined
    at Array.stream [as 3] (/opt/nor-web-proxy/node_modules/http-proxy/lib/http-proxy/passes/web-incoming.js:103:42)
    at ProxyServer.web (/opt/nor-web-proxy/node_modules/http-proxy/lib/http-proxy/index.js:76:21)
    at Server.<anonymous> (/opt/nor-web-proxy/test.js:10:9)
    at Server.EventEmitter.emit (events.js:98:17)
    at HTTPParser.parser.onIncoming (http.js:2076:12)
    at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:120:23)
    at Socket.socket.ondata (http.js:1966:22)
    at TCP.onread (net.js:525:27)

I am running node v0.10.20.

@matejkramny
Copy link

👍 same issue. Probably solve it by passing an object to http.createProyx...

pass this:

{
    target: {
        protocol: 'http:'
    }
}

@thejhh
Copy link
Author

thejhh commented Oct 15, 2013

I don't think it fixes the actual problem.

In my case I must change the proxy.options each request and the code seems not to support that at the moment.

The problem is here at lib/http-proxy/index.js line 76, there is cbl ? false : this, where -- AFAIK -- this is the instance of HTTP proxy object proxy, which has proxy.options but it's not the options which is created at lib/http-proxy/index.js line 48.

I think it might be fixed by changing that code on line 76 to:

cbl ? false : {emit:this.emit.bind(this), options:options}

...but that feels like a bad kind of workaround.

@thejhh
Copy link
Author

thejhh commented Oct 15, 2013

Actually I don't see how that proxy.options would be url parsed since it is done at line 63 for different options which seems not to be in use at all.

Although it's hard to understand how that could be possible -- nobody is using the code as it is documented?

thejhh added a commit to sendanor/node-http-proxy that referenced this issue Oct 15, 2013
@thejhh
Copy link
Author

thejhh commented Oct 15, 2013

I've tested this workaround and it actually does fix the issue I'm having.

However I don't know which would be best way to fix it in real life. Currently the passes implementation for web does not use anything else but .options and .emit, so this workaround works at least for it.

@thejhh
Copy link
Author

thejhh commented Oct 15, 2013

Now that I rechecked the code for missing options.xfwd support I see that all other passes are using options -- not instance of proxy -- as the third option. Isn't this wrong behavior to be different?

@yawnt
Copy link
Contributor

yawnt commented Oct 16, 2013

sorry for the late answer, i have been refactoring some code and might have inadvertedly broken something.. i'll look into it this afternoon

@thejhh
Copy link
Author

thejhh commented Oct 16, 2013

Another issue I noticed was that the code is changing req.headers directly which is documented as read only in Node.js 0.10.20 -- although I haven't tested if Node still lets you change them.

@cronopio
Copy link
Contributor

Using the last commit (f720e36) from the caronte branch show that this is no more an issue. If you saw it again please reopen this.

@wookets
Copy link

wookets commented Oct 31, 2013

Hi.

I'm seeing this issue and it seems to revolve around the following code in lib/http-proxy/index.js - line 77. All of the above code uses options, but the very last part references this.options, which is just an empty object.

 if(passes[i](req, res, this.options, head, cbl ? false : this, cbl)) { // passes can return a truthy value to halt the loop
      break;
    }

If I change this.options to just options, it works on my end.

@cronopio
Copy link
Contributor

cronopio commented Nov 2, 2013

The this.options is a reference to the options passed to the construction, See https://github.com/nodejitsu/node-http-proxy/blob/caronte/lib/http-proxy/index.js#L90 So, if at some point that reference is an empty object so is a bug. Please open a new issue with further information and examples code to reflect the bug.

@jcrugzz
Copy link
Contributor

jcrugzz commented Nov 5, 2013

Yes this is a bug, currently filed in #510

@q2dg
Copy link

q2dg commented Jun 8, 2014

Why is it closed? The workaround works, but the problem (simply running basic example code from main github page) remains unsolved (Node 0.11.13)

@yankeeinlondon
Copy link

I too am still having this problem

@theak
Copy link

theak commented Jul 16, 2014

Seeing this issue currently as well

@jcrugzz
Copy link
Contributor

jcrugzz commented Jul 16, 2014

@theak can you post a new issue and give an example of the code you are using? This shouldn't be happening

@theak
Copy link

theak commented Jul 16, 2014

Ah, actually turned out to be an issue with how I was calling the code. Everything seems to be working fine now- false alarm 👍

@qmacro
Copy link

qmacro commented Jul 27, 2014

Hello. I'm having exactly the same issue; I've cloned this repo & npm installed it. I've copy-pasted the sample from http://blog.nodejitsu.com/node-http-proxy-1dot0/ which looks like this:

var httpProxy = require('http-proxy')

var proxy = httpProxy.createProxy();

var options = {  
  'foo.com': 'website.com:8001',
  'bar.com': 'website2.com:8002'
}

require('http').createServer(function(req, res) {  
  proxy.web(req, res, {
    target: options[req.headers.host]
  });
}).listen(8000);

When I visit http://localhost:8000 the following error occurs:

/Users/dj/ui5/node_modules/http-proxy/lib/http-proxy/passes/web-incoming.js:109
    var proxyReq = (options.target.protocol === 'https:' ? https : http).reque
                                  ^
TypeError: Cannot read property 'protocol' of undefined
    at Array.stream [as 3] (/Users/dj/ui5/node_modules/http-proxy/lib/http-proxy/passes/web-incoming.js:109:35)
    at ProxyServer.<anonymous> (/Users/dj/ui5/node_modules/http-proxy/lib/http-proxy/index.js:78:21)
    at Server.<anonymous> (/Users/dj/ui5/proxy.js:11:9)
    at Server.EventEmitter.emit (events.js:98:17)
    at HTTPParser.parser.onIncoming (http.js:2108:12)
    at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:121:23)
    at Socket.socket.ondata (http.js:1966:22)
    at TCP.onread (net.js:527:27)

Perhaps I'm just having a bad day, but am I missing something?

Thanks.

@jcrugzz
Copy link
Contributor

jcrugzz commented Jul 29, 2014

@qmacro hey there was a typo in the blog post unfortunately that I just fixed. It should be a full URL prefixed with http:// or https:// if it will be a string passed in as target. We should have a check for this though.

@webduvet
Copy link

webduvet commented Aug 7, 2014

@qmacro Hi I was playing with the similar issue you came across. In my case when I entered my IP address the proxy generated the very same error. Than I included the my IPaddress in target options and redirected the request to some error server.

var options = {
   'IPaddress:80': 'website.com:3456', //this point to app looking after errors in my case
   'foo.com': 'website.com:8001',
   'bar.com': 'website2.com:8002'
}

@rjcorwin
Copy link

@webduvet That IP address issue took me some time to figure out. That was as tough one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests