Skip to content

Instantly share code, notes, and snippets.

@stash
Created March 15, 2012 17:48
Show Gist options
  • Select an option

  • Save stash/2045588 to your computer and use it in GitHub Desktop.

Select an option

Save stash/2045588 to your computer and use it in GitHub Desktop.
HTTP proxy in 5 minutes
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);
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