{"issue":{"user":{"id":681213,"login":"taris","url":"https://api.github.com/users/taris","avatar_url":"https://secure.gravatar.com/avatar/e842fc09517a25b0b2646db2698ade8d?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","gravatar_id":"e842fc09517a25b0b2646db2698ade8d"},"milestone":null,"created_at":"2011-12-15T15:28:34Z","title":"proxy does not work within a callback function","state":"open","updated_at":"2011-12-16T11:17:03Z","labels":[],"id":2567923,"html_url":"https://github.com/nodejitsu/node-http-proxy/issues/168","closed_at":null,"pull_request":{"patch_url":null,"html_url":null,"diff_url":null},"body":"I am trying to call \"proxy.proxyRequest()\" within a callback function after reading some data, but this does not work at all (or at least i can't see my mistake :) )\r\n\r\nTry to use my code below - with the request url \"http://localhost:8000/\" everything is fine and the request will be routed to the target server (same script - port 9000).\r\nBut when you try to load something with the url \"http://localhost:8000/no-routing\" first (or save or anything that needs a callback function), the target server never receives a request, no error will be raised and the client never receives a response and stays pending.\r\nNevertheless the function \"proxyRequest()\" and the node.js function \"http.request()\" will be called, but nothing goes to the target server.\r\n\r\nHope it stays an easy issue - any help is greatly appreciated :)\r\nCheers!\r\n\r\n```javascript\r\nvar http = require('http'),\r\n    httpProxy = require('http-proxy'),\r\n    fs = require('fs');\r\n\r\n//\r\n// Create a proxy server with custom application logic\r\n//\r\nhttpProxy.createServer(function (req, res, proxy) {\r\n  //\r\n  // Put your custom server logic here\r\n  //\r\n  console.log(\"##### PROXY: NEW REQUEST #####\");\r\n\r\n  if(req.url == '/no-routing') {\r\n      /**\r\n       * In a callback function the target server never receives a request and client keeps on \"pending\"\r\n       */\r\n      fs.readdir(__dirname, function(err, files)\r\n      {\r\n        console.dir(proxy);\r\n        proxy.proxyRequest(req, res, {\r\n            host: 'localhost',\r\n            port: 9000\r\n        });\r\n      });\r\n  } else {\r\n    /**\r\n     * Without a callback function the request will be routed to target server\r\n     */\r\n    console.dir(proxy);\r\n    proxy.proxyRequest(req, res, {\r\n        host: 'localhost',\r\n        port: 9000\r\n    });\r\n  }\r\n}).listen(8000);\r\n\r\nhttp.createServer(function (req, res) {\r\n  res.writeHead(200, { 'Content-Type': 'text/plain' });\r\n  res.write('request successfully proxied: ' + req.url +'\\n' + JSON.stringify(req.headers, true, 2));\r\n  res.end();\r\n\r\n  console.log(\"##### TARGET: REQUEST RECEIVED #####\");\r\n}).listen(9000);\r\n\r\nconsole.log(\"---\\nProxy listens on port 8000 and routes anything to port 9000\");\r\nconsole.log(\"Without callback and problems: http://localhost:8000/\");\r\nconsole.log(\"With a callback and no routing: http://localhost:8000/no-routing\\n---\");\r\n```","number":168,"url":"https://api.github.com/repos/nodejitsu/node-http-proxy/issues/168","comments":2,"assignee":null},"comment":{"user":{"id":681213,"login":"taris","url":"https://api.github.com/users/taris","avatar_url":"https://secure.gravatar.com/avatar/e842fc09517a25b0b2646db2698ade8d?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","gravatar_id":"e842fc09517a25b0b2646db2698ade8d"},"created_at":"2011-12-16T11:17:02Z","updated_at":"2011-12-16T11:17:02Z","id":3176027,"body":"It does work, when you use http.request() directly and i replaced node-http-proxy with an own small proxy module for now.\r\nMaybe someone finds a bug or points me to my failtrain :D","url":"https://api.github.com/repos/nodejitsu/node-http-proxy/issues/comments/3176027"},"action":"created"}