Skip to content

Instantly share code, notes, and snippets.

@AvianFlu
Created August 12, 2011 18:54
Show Gist options
  • Save AvianFlu/1142699 to your computer and use it in GitHub Desktop.
Save AvianFlu/1142699 to your computer and use it in GitHub Desktop.
A routing table and a forward in the same proxy setup
var http = require('http'),
httpProxy = require('http-proxy');
httpProxy.createServer(function (req, res, proxy) {
var options = {};
if (req.headers.host === 'google.myserver.com') {
req.headers.host = 'google.com';
options.host = 'google.com';
options.port = 80;
console.log(req.headers);
proxy.proxyRequest(req, res, options);
}
else {
console.log(req.headers);
proxy.proxyRequest(req, res);
}
},
{
router: {
'myserver.com': 'localhost:9090',
'mail.myserver.com': 'localhost:9191'
}
}
).listen(80);
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('request successfully proxied: ' + req.url +'\n' + JSON.stringify(req.headers, true, 2));
res.end();
}).listen(9090);
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('request successfully proxied: ' + req.url +'\n' + JSON.stringify(req.headers, true, 2));
res.end();
}).listen(9191);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment