Last active
August 29, 2015 14:23
-
-
Save blumonkey/2b4ab631127bd9e66ede to your computer and use it in GitHub Desktop.
[NODEJS] Simple CORS proxy that works within a corporate Proxy
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'); | |
var url = require('url'); | |
var request = require('request'); | |
/* | |
Start server by `nodejs thisfile.js` | |
Usage: goto "localhost:1337?url=http://www.google.com" to proxify google | |
*/ | |
//Custom set the PORT, or you can use one defined in the env. | |
http.createServer(onRequest).listen(1337); | |
function onRequest(req, res) { | |
//setting the CORS headers. | |
res.setHeader('Access-Control-Allow-Origin', '*'); | |
res.setHeader('Access-Control-Request-Method', '*'); | |
res.setHeader('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE'); | |
res.setHeader('Access-Control-Allow-Headers', 'Content-Type'); | |
var queryData = url.parse(req.url, true).query; | |
if (queryData.url) { | |
//Optional PROXY if behind a Proxy-Server, note that Http and Https are both | |
//piped through http://proxy:8080 in this case | |
request({'url':queryData.url, | |
'proxy':'http://10.3.100.207:8080'}).on('error', function(e) { | |
res.end(e+''); | |
}).pipe(res); | |
} | |
else { | |
res.end("This is the homepage!, try localhost:1337?url=http://www.google.com to goto google!"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment