Skip to content

Instantly share code, notes, and snippets.

@joshperry
Created October 8, 2014 21:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshperry/b32559be5a2e05d51c53 to your computer and use it in GitHub Desktop.
Save joshperry/b32559be5a2e05d51c53 to your computer and use it in GitHub Desktop.
HTTP client proxy
var http = require('http'),
EventEmitter = require('event').EventEmitter,
httpProxy = require('http-proxy');
var proxyAgent = new http.Agent({ maxSockets: 3 });
proxyAgent.createConnection = function(options) {
var clientSocket = registry.getSocket(options.servername);
if(!clientSocket) {
// Set clientSocket to a future Socket here somehow
}
return clientSocket;
}
// Create a proxy dispatcher with our custom agent
var proxy = httpProxy.createProxyServer({agent: proxyAgent, ws: true});
// Just call `proxy.web()` to proxy a web request to the target passed in the options
// also you can use `proxy.ws()` to proxy a websockets request
var server = http.createServer(function(req, res) {
proxy.web(req, res, { target: 'http://' + req.hostname });
});
console.log("listening on port 5050")
server.listen(5050);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment