Created
May 27, 2012 05:28
-
-
Save ammmir/2802271 to your computer and use it in GitHub Desktop.
simple HTTP proxy server
This file contains hidden or 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'), | |
url = require('url'); | |
var PORT = process.argv[2] || 1337; | |
process.on('uncaughtException', function(error) { | |
console.error('uncaughtException', error); | |
}); | |
http.createServer(function(req, res) { | |
console.log(req.connection.remoteAddress + ": " + req.method + " " + req.url); | |
var target_url = url.parse(req.url); | |
var proxy = http.createClient(target_url.port, target_url.hostname); | |
var proxy_req = proxy.request(req.method, target_url.path, req.headers); | |
proxy_req.addListener('response', function(proxy_res) { | |
proxy_res.addListener('data', function(chunk) { | |
res.write(chunk, 'binary'); | |
}); | |
proxy_res.addListener('end', function() { | |
res.end(); | |
}); | |
res.writeHead(proxy_res.statusCode, proxy_res.headers); | |
}); | |
req.addListener('data', function(chunk) { | |
proxy_req.write(chunk, 'binary'); | |
}); | |
req.addListener('end', function() { | |
proxy_req.end(); | |
}); | |
}).listen(PORT, function() { | |
console.log('Proxy server listening on port %d', PORT); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment