Created
August 12, 2016 05:58
-
-
Save aniruddhanath/fab9b64fe4440d06178540e86e9fd1e3 to your computer and use it in GitHub Desktop.
Simple HTTP Proxy Server with Node.js
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
const http = require('http'); | |
const url = require('url'); | |
http.createServer(onRequest).listen(3000); | |
function onRequest (req, res) { | |
console.log('serving: ' + req.url); | |
const u = url.parse(req.url); | |
const options = { | |
hostname: u.hostname, | |
port: 80, | |
path: u.path, | |
method: 'GET' | |
}; | |
var proxy = http.request(options, function (proxy_res) { | |
proxy_res.pipe(res, { | |
end: true | |
}); | |
}); | |
req.pipe(proxy, { | |
end: true | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment