Created
March 15, 2012 17:48
-
-
Save stash/2045588 to your computer and use it in GitHub Desktop.
HTTP proxy in 5 minutes
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 http = require('http'); | |
| var server = http.createServer(function(req,res) { | |
| var opts = { | |
| method: 'GET', host: 'news.ycombinator.com', port: 80, path: req.url | |
| }; | |
| var proxyReq = http.request(opts, function(proxyRes) { | |
| var chunks = ''; | |
| proxyRes.on('data', function(chunk) { | |
| chunks += chunk; | |
| }); | |
| proxyRes.on('end', function() { | |
| var modified = chunks | |
| .replace(/Hacker News/g, "Hax0r N3ws") | |
| .replace(/ comments?/g,' jibber-jabber'); | |
| res.end(modified,'utf8'); | |
| }); | |
| res.writeHead(proxyRes.statusCode, proxyRes.headers); | |
| }); | |
| proxyReq.end(); | |
| }); | |
| server.listen(8888); |
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 http = require('http'); | |
| var server = http.createServer(function(req,res) { | |
| res.writeHead(200,{'content-type':'text/plain'}); | |
| res.write("it's now "+new Date()); | |
| res.end(); | |
| }); | |
| server.listen(8888, 'localhost'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment