Skip to content
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

Cannot call method 'split' of undefined #800

Closed
efkan opened this issue Apr 6, 2015 · 10 comments
Closed

Cannot call method 'split' of undefined #800

efkan opened this issue Apr 6, 2015 · 10 comments

Comments

@efkan
Copy link

efkan commented Apr 6, 2015

I'm a newbie to node-http-proxy module.

my aim

I need to use the module provide multi-SSL for multi-subdomain.
For example;
if a user call process.localhost:1443 then I should route the call to process.localhost:2443 and
if a user call api.localhost:1443 then I should route the call to api.localhost:3443

what's happening

I wrote the below server.js codes.
If I change httpProxy.createServer(options) line with httpProxy.createServer({target:'http://process.localhost:2443'}) then it works properly!

Otherwise when I try to call process.localhost:1443 I get the following error;

D:\Work Space\...\http-proxy\node_modules\requires-port\index.js:13
protocol = protocol.split(':')[0];
TypeError: Cannot call method 'split' of undefined

protocol seems as undefined.

function required(port, protocol) {
  protocol = protocol.split(':')[0];

What should I do?

server.js

var fs = require('fs'),
    httpProxy = require('http-proxy'),
    express = require('express'),
    app = require('./app').service,
    api = require('./api').service;

// PROXY
var options = {
  changeOrigin: true,
  forward: {
    'process.localhost': 'process.localhost:2443',
    'api.localhost' : 'api.localhost:3443'
  }
}

httpProxy.createServer(options).listen(1443, function() {
  console.log('Proxy is listening on port 1443')
})


// HTTP
app
.listen(2443, function() {
  console.log('PROCESS APP server is listening on port 2443')
})

api
.listen(3443, function() {
  console.log('API APP server is listening on port 3443')
})
@efkan efkan changed the title Error: connect ECONNREFUSED Cannot call method 'split' of undefined Apr 6, 2015
@damonmcminn
Copy link
Contributor

@efkan It's possible the source of your woes is options.target being an object that maps target string to forward string. The comment says it expects a string:
https://github.com/nodejitsu/node-http-proxy/blob/master/lib/http-proxy.js#L33

@efkan
Copy link
Author

efkan commented Apr 6, 2015

Thank you very much,

I've spent 11 hours today but I guess I have to spent much more time to learn the use.
I'm studying on a regarding targetand forward file.
https://github.com/nodejitsu/node-http-proxy/blob/master/examples/http/forward-and-target-proxy.js

I just want a simple conditional route operation.
If api.mydomain.com:1443 is called route api.mydomain.com:2443
and If process.mydomain.com:1443 is called route process.mydomain.com:2443

Because I have to use only one port [iptables 443 -> 1443] for two different subdomains (with two certificates). But I couldn't the most basic this operation yet!

Actually I read comment that you've indicated. But unfortunately I couldn't understand.

@efkan
Copy link
Author

efkan commented Apr 7, 2015

I've simplified my question and I added my new experiences..

@damonmcminn
Copy link
Contributor

@efkan It's necessary for you to write the logic yourself. For example:

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

var targets = {
  'process.localhost': 'process.localhost:2443',
  'api.localhost' : 'api.localhost:3443'
};

var proxy = httpProxy.createServer({changeOrigin: true})

http.createServer(function(req, res) {
  /* target NOT forward
  *  target === final server to receive request
  *  forward === another proxy server to pass request through
  */
  var options = {
    target: targets[req.headers.host]
  };
  proxy.web(req, res, options, errorCallback); // errorCallback is optional
}).listen(1443);

I have had a similar use case, but using a single domain and the first path to identify where to proxy requests. You can read it here: https://github.com/damonmcminn/api-proxy

@efkan
Copy link
Author

efkan commented Apr 7, 2015

Really thanks @damonmcminn 🌟 !

I understood http-proxy module now conceptually.
And your API-Proxy application will be a guide for me.

Thank you again for your helps..

ps: I got an error as Must provide a proper URL as target 😭 however really it is not important now

@efkan efkan closed this as completed Apr 7, 2015
@damonmcminn
Copy link
Contributor

@efkan No problem!

The error is because targets do not have a protocol. Woops!
Change them for example: process.localhost:2443 -> http://process.localhost:2443

@efkan
Copy link
Author

efkan commented Apr 7, 2015

Thank you again! You made my day!

Probably there is another problem with my codes or http-proxy or anything else. Because I added the protocol but console gives the same error.

It indicates;

`..\..http-proxy\lib\http-proxy\index.js:68:35`
   throw err;

Error: Must provide a proper URL as target
      at ProxyServer.<anonymous> (..\..http-proxy\lib\http-proxy\index.js:68:35)
      at Server.<anonymous> (..\..\my project\server.js:56:9)

and server.js line 56 is;
proxy.web(req, res, options); // errorCallback is optional

and my targets are;

var targets = {
  'http://process.localhost': 'http://process.localhost:2443',
  'http://api.localhost' : 'http://api.localhost:3443'
};

@damonmcminn
Copy link
Contributor

Your targets should be in this form:

{ 'sub.domain': 'protocol://sub.domain:port' }

This is because req.headers.host is a string that represents the domain (i.e. it is not a URL) which is used as a key to retrieve the target URL.

For example:

{ 'api.localhost': 'http://api.localhost:3443' }

Does this make sense?

@efkan
Copy link
Author

efkan commented Apr 7, 2015

I see, OK. Thank you again...

I checked it and I saw req.headers.host returns hostname with port number. So I added the port number and it solved thanks to you

I owe you a coffee 😃

{ 'api.localhost:1443': 'http://api.localhost:3443' }

@damonmcminn
Copy link
Contributor

No problem :)

On 7 April 2015 at 11:36, efkan notifications@github.com wrote:

I see, OK. Thank you again...


Reply to this email directly or view it on GitHub
#800 (comment)
.

{
name: Damon McMinn,
what: JavaScript developer,
tel: 07473119510,
links: [GitHub https://github.com/damonmcminn, self
http://damonmcminn.com/],
location: London
}

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

2 participants