Skip to content

Instantly share code, notes, and snippets.

@ammmir
Created May 27, 2012 05:28
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 ammmir/2802271 to your computer and use it in GitHub Desktop.
Save ammmir/2802271 to your computer and use it in GitHub Desktop.
simple HTTP proxy server
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