Skip to content

Instantly share code, notes, and snippets.

@rjack
Created April 19, 2011 17:06
Show Gist options
  • Save rjack/928850 to your computer and use it in GitHub Desktop.
Save rjack/928850 to your computer and use it in GitHub Desktop.
Giving node-http-proxy a try
var http = require("http"),
url = require("url");
var server = http.createServer(function (req, res) {
console.log("connection count:" + server.connections);
var fwd_url = url.parse(req.url),
fwd_req_opts = {
agent: false,
host: fwd_url.hostname,
port: fwd_url.port || 80,
path: fwd_url.pathname + (fwd_url.search || ""),
headers: req.headers,
method: req.method,
},
fwd_req = http.request(fwd_req_opts, function (fwd_res) {
//console.log("req", req.url);
res.writeHead(fwd_res.statusCode, fwd_res.headers);
fwd_res.on("data", function (chunk) {
res.write(chunk);
});
fwd_res.on("end", function () {
//console.log("end", req.url);
res.end();
});
});
req.on("data", function (chunk) {
fwd_req.write(chunk);
});
req.on("end", function () {
fwd_req.end();
});
});
server.listen(9000);
var http = require('http'),
httpProxy = require('http-proxy'),
url = require("url");
httpProxy.createServer(function (req, res, proxy) {
var req_url = url.parse(req.url);
proxy.proxyRequest(req, res, {
host: req_url.hostname,
port: req_url.port
});
console.log(req.url);
}).listen(9000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment