Guest User

Untitled

a guest
May 26th, 2015
305
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var http = require("http");
  2. var net = require("net");
  3.  
  4. var httpAddress = "0.0.0.0";
  5. var httpPort = "8001";
  6.  
  7. var getAgent = function() {
  8. return undefined;
  9. var agent;
  10. return function() {
  11. if (!agent) {
  12. agent = new http.Agent();
  13. agent.createConnection = function(options) {
  14. var socket = net.createConnection(options);
  15. socket.on("error", function(err) {
  16. console.log("AHA", err);
  17. });
  18. return socket;
  19. }
  20. }
  21. return agent;
  22. }();
  23. }
  24.  
  25. server = http.createServer(function (req, res) {
  26. console.log("GOT REQUEST");
  27.  
  28. var agent = getAgent();
  29. var postBody = JSON.stringify({});
  30. var options = {
  31. hostname: '192.168.1.11',
  32. port: 80,
  33. path: '/upload',
  34. method: 'POST',
  35. headers: {
  36. 'Content-Type': 'application/json',
  37. 'Content-Length': postBody.length
  38. },
  39. agent: agent,
  40. };
  41.  
  42. var backendRequest = http.request(options, function(res) {
  43. console.log("GOT RESPONSE");
  44. });
  45. backendRequest.on("socket", function(socket) {
  46. console.log("GOT SOCKET");
  47. socket.on("error", function(err) {
  48. console.log("SOCKET ERROR", err);
  49. });
  50. });
  51. backendRequest.on("connect", function() {
  52. console.log("CONNECT");
  53. });
  54. backendRequest.on("error", function(e) {
  55. console.log("ERR", e);
  56. res.end("watt");
  57. });
  58. backendRequest.write(postBody);
  59. });
  60.  
  61. server.listen(httpPort, httpAddress);
  62. console.log("Server running at http://<ip-address>:" + httpPort + "/");
  63.  
  64. // execute "curl --max-time 5 http://localhost:8001; curl --max-time 5 http://localhost:8001" on console to trigger error
RAW Paste Data