Created
May 20, 2013 16:58
-
-
Save brianswisher/5613576 to your computer and use it in GitHub Desktop.
Bare bones NodeJs url proxy
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 sys = require('sys'), | |
http = require('http'), | |
url = require('url'); | |
function notFound(res) { | |
res.writeHead(404, "text/plain"); | |
res.end("404: File not found"); | |
} | |
http.createServer(function(b_req, b_res){ | |
var b_url = url.parse(b_req.url, true); | |
if(!b_url.query || !b_url.query.url) return notFound(b_res); | |
var p_url = url.parse(b_url.query.url); | |
var p_client = http.request({ | |
method: 'GET', | |
path: p_url.pathname || '/', | |
port: 80, | |
host: p_url.hostname | |
},function(res){ | |
b_res.writeHead(res.statusCode, res.headers); | |
res.on('data', function (chunk) { | |
b_res.write(chunk); | |
}); | |
res.on('end', function (chunk) { | |
b_res.end(); | |
}); | |
}); | |
p_client.end(); | |
}).listen(3000, "127.0.0.1"); | |
sys.puts("Server running at http://127.0.0.1:3000"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment