Created
August 4, 2011 05:04
-
-
Save fennb/1124535 to your computer and use it in GitHub Desktop.
node.js proxy server in 20 lines of JS (via http://www.catonmat.net/http-proxy-in-nodejs/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'); | |
http.createServer(function(request, response) { | |
var proxy = http.createClient(80, request.headers['host']) | |
var proxy_request = proxy.request(request.method, request.url, request.headers); | |
proxy_request.addListener('response', function (proxy_response) { | |
proxy_response.addListener('data', function(chunk) { | |
response.write(chunk, 'binary'); | |
}); | |
proxy_response.addListener('end', function() { | |
response.end(); | |
}); | |
response.writeHead(proxy_response.statusCode, proxy_response.headers); | |
}); | |
request.addListener('data', function(chunk) { | |
proxy_request.write(chunk, 'binary'); | |
}); | |
request.addListener('end', function() { | |
proxy_request.end(); | |
}); | |
}).listen(8080); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment