Skip to content

Instantly share code, notes, and snippets.

@atmos
Forked from pedro/test.js
Created May 21, 2010 00:37
Show Gist options
  • Save atmos/408327 to your computer and use it in GitHub Desktop.
Save atmos/408327 to your computer and use it in GitHub Desktop.
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