Created
March 10, 2011 17:53
-
-
Save drewwells/864549 to your computer and use it in GitHub Desktop.
This demo works for basic data, generic requests ie url=http://google.com don't work
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'), | |
httpProxy = require('http-proxy'), | |
url = require('url'); | |
// Create your proxy server | |
httpProxy.createServer(function(req, res, proxy){ | |
var baseUrl = url.parse(req.url, true), | |
query = baseUrl.query, | |
server = !!query.url ? query.url.replace(/http:\/\/([^:\/]+).*/i,function(url,server){return server;}) : '', | |
port = !!query.url ? query.url.match(/:(\d+)/) : 80; | |
if( !server ) return; | |
if( port && port.length > 1 ){ | |
port = port[1]; | |
} else { | |
port = 80; | |
} | |
req.url = req.url.replace(/\?url=http:\/\/[^/]+\//,''); | |
req.headers.host = server + ":" + port; | |
console.log(req) | |
proxy.proxyRequest(port, server, req); | |
}).listen(8000); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage:
http://127.0.0.1:8000/?url=http://www.google.com/calendar/feeds/developer-calendar@google.com/public/full?alt=json
Refer to https://github.com/nodejitsu/node-http-proxy#readme to get node-http-proxy running