-
-
Save atmos/408327 to your computer and use it in GitHub Desktop.
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 sys = require('sys'), | |
http = require('http'), | |
spawn = require('child_process').spawn | |
function passAll(callback) { | |
callback() | |
} | |
function passAfterRequest(callback) { | |
var node = http.createClient(80, 'nodejs.org'); | |
var nodeReq = node.request('GET', '/', { 'host': 'nodejs.org' }); | |
nodeReq.addListener('response', function (site1Res) { | |
sys.puts('got a response from nodejs.org') | |
callback() | |
}) | |
nodeReq.end() | |
} | |
function passAfterSpawning(callback) { | |
var ls = spawn('ls') | |
ls.addListener('exit', function(code) { | |
sys.puts('ls exited with ' + code) | |
callback() | |
}) | |
} | |
http.createServer(function(req, res) { | |
// passAfterSpawning(function() { // change to passAfterRequest and watch it break | |
passAfterRequest(function() { // change to passAfterRequest and watch it break | |
sys.puts('proxying google.com') | |
var google = http.createClient(80, 'google.com'); | |
var googleReq = google.request('GET', '/', { 'host': 'google.com' }); | |
googleReq.addListener('response', function (googleRes) { | |
res.writeHead(googleRes.statusCode, googleRes.headers) | |
googleRes.addListener('data', function(chunk) { | |
sys.puts('\trecv ' + chunk.length) | |
res.write(chunk, 'binary') | |
}) | |
googleRes.addListener('end', function() { | |
res.end() | |
}) | |
}) | |
req.addListener('data', function(chunk) { | |
sys.puts('\tsend ' + chunk.length) | |
googleReq.write(chunk, 'binary') | |
}) | |
googleReq.end() | |
}) | |
}).listen(8000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment