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

the example doesn't work #3

Open
hareeqi opened this issue Mar 22, 2017 · 1 comment
Open

the example doesn't work #3

hareeqi opened this issue Mar 22, 2017 · 1 comment

Comments

@hareeqi
Copy link

hareeqi commented Mar 22, 2017

I used the package published in NPM and it throws

TypeError: Cannot read property 'target' of undefined

options.target = options.target || this.target;

my code

var net = require('net');
var TcpProxy = require('tcp-proxy');

var targets = [
  {
    host: 'localhost',
    port: 5050
  },
  {
    host: 'localhost',
    port: 5050
  }
];

var proxies = targets.map(function (target) {
  return new TcpProxy({
    target: target
  });
});

var nextProxy = function () {
  var proxy = proxies.shift();
  proxies.push(proxy);
  return proxy;
};

var server = net.createServer(function(socket) {
  nextProxy().proxy(socket);
});

server.listen(8000);
@hareeqi
Copy link
Author

hareeqi commented Mar 24, 2017

this should fix the example

var net = require('net');
var TcpProxy = require('tcp-proxy');

var targets = [
  {
    host: 'localhost',
    port: 5050
  },
  {
    host: 'localhost',
    port: 5050
  }
];

var nextTarget = function () {
  var target = targets.shift();
  targets.push(target);
  return {target:target};
};

var server = net.createServer(function(socket) {
  new TcpProxy(socket,nextTarget())
});

server.listen(8000);

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

1 participant