Skip to content

Using .createProxy() Errors When Used #553

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
Albert-IV opened this issue Jan 17, 2014 · 5 comments
Closed

Using .createProxy() Errors When Used #553

Albert-IV opened this issue Jan 17, 2014 · 5 comments

Comments

@Albert-IV
Copy link

From your examples on the Nodejitsu blog, the following code should redirect correctly:

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

var proxy = httpProxy.createProxy();


http.createServer(function(req, res) {
  res.end('hi!');
}).listen(3000);


require('http').createServer(function(req, res) {
  proxy.web(req, res, { target: 'http://127.0.0.1:3000' });
}).listen(8000);

When you hit http://localhost:8000, the proxy process dies with this error:

TypeError: Cannot read property 'length' of undefined

Node v0.10.24, http-proxy v1.0.0

@Albert-IV
Copy link
Author

To clarify, the createProxyServer() method works as expected.

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

httpProxy.createProxyServer({
  target: 'http://127.0.0.1:3000',
  ws    : true
}).listen(80);

Unfortunately this method doesn't allow you to filter by host.

@yawnt yawnt closed this as completed in 53a2653 Jan 17, 2014
@yawnt
Copy link
Contributor

yawnt commented Jan 17, 2014

thanks for reporting, fixed in [email protected]

@Albert-IV
Copy link
Author

Not a problem.

@yawnt
Copy link
Contributor

yawnt commented Jan 17, 2014

(for the record, you can use the same methods on .createProxyServer().. it will work in the same way .web and ws .. there's a .createProxy() alone because it's more clean semantically :P)

@Albert-IV
Copy link
Author

Right on. I was trying to proxy to different ports depending on the req.headers.host, so from what I understand you have to use the .createProxy over .createProxyServer For example, this will error out:

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

var options = {
  'localhost': 'http://127.0.0.1:3000',
  'beta.localhost': 'http://127.0.0.1:3001'
}

var proxy = httpProxy.createProxyServer(function(req, res) {
  proxy.web(req, res, {
    target: options[req.headers.host]
  }, function(e) {
  });
}).listen(80);

However this works:

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

var proxy = httpProxy.createProxy();

var options = {
  'localhost': 'http://127.0.0.1:3000',
  'beta.localhost': 'http://127.0.0.1:3001'
}

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

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