Guest User

Untitled

a guest
May 26th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. var sys = require('sys'),
  2. http = require('http'),
  3. spawn = require('child_process').spawn
  4.  
  5. function passAll(callback) {
  6. callback()
  7. }
  8.  
  9. function passAfterRequest(callback) {
  10. var node = http.createClient(80, 'nodejs.org');
  11. var nodeReq = node.request('GET', '/', { 'host': 'nodejs.org' });
  12.  
  13. nodeReq.addListener('response', function (site1Res) {
  14. sys.puts('got a response from nodejs.org')
  15. callback()
  16. })
  17. nodeReq.end()
  18. }
  19.  
  20. function passAfterSpawning(callback) {
  21. var ls = spawn('ls')
  22. ls.addListener('exit', function(code) {
  23. sys.puts('ls exited with ' + code)
  24. callback()
  25. })
  26. }
  27.  
  28. http.createServer(function(req, res) {
  29. // passAfterSpawning(function() { // change to passAfterRequest and watch it break
  30. passAfterRequest(function() { // change to passAfterRequest and watch it break
  31. sys.puts('proxying google.com')
  32. var google = http.createClient(80, 'google.com');
  33. var googleReq = google.request('GET', '/', { 'host': 'google.com' });
  34.  
  35. googleReq.addListener('response', function (googleRes) {
  36. res.writeHead(googleRes.statusCode, googleRes.headers)
  37. googleRes.addListener('data', function(chunk) {
  38. sys.puts('\trecv ' + chunk.length)
  39. res.write(chunk, 'binary')
  40. })
  41. googleRes.addListener('end', function() {
  42. res.end()
  43. })
  44. })
  45.  
  46. req.addListener('data', function(chunk) {
  47. sys.puts('\tsend ' + chunk.length)
  48. googleReq.write(chunk, 'binary')
  49. })
  50. googleReq.end()
  51. })
  52. }).listen(8000)
Add Comment
Please, Sign In to add comment