Skip to content

proxy does not work within a callback function #168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
taris opened this issue Dec 15, 2011 · 4 comments
Closed

proxy does not work within a callback function #168

taris opened this issue Dec 15, 2011 · 4 comments

Comments

@taris
Copy link

taris commented Dec 15, 2011

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 :) )

Try 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).
But 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.
Nevertheless the function "proxyRequest()" and the node.js function "http.request()" will be called, but nothing goes to the target server.

Hope it stays an easy issue - any help is greatly appreciated :)
Cheers!

var http = require('http'),
    httpProxy = require('http-proxy'),
    fs = require('fs');

//
// Create a proxy server with custom application logic
//
httpProxy.createServer(function (req, res, proxy) {
  //
  // Put your custom server logic here
  //
  console.log("##### PROXY: NEW REQUEST #####");

  if(req.url == '/no-routing') {
      /**
       * In a callback function the target server never receives a request and client keeps on "pending"
       */
      fs.readdir(__dirname, function(err, files)
      {
        console.dir(proxy);
        proxy.proxyRequest(req, res, {
            host: 'localhost',
            port: 9000
        });
      });
  } else {
    /**
     * Without a callback function the request will be routed to target server
     */
    console.dir(proxy);
    proxy.proxyRequest(req, res, {
        host: 'localhost',
        port: 9000
    });
  }
}).listen(8000);

http.createServer(function (req, res) {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.write('request successfully proxied: ' + req.url +'\n' + JSON.stringify(req.headers, true, 2));
  res.end();

  console.log("##### TARGET: REQUEST RECEIVED #####");
}).listen(9000);

console.log("---\nProxy listens on port 8000 and routes anything to port 9000");
console.log("Without callback and problems: http://localhost:8000/");
console.log("With a callback and no routing: http://localhost:8000/no-routing\n---");
@taris
Copy link
Author

taris commented Dec 15, 2011

As a sidenote: tried it the same way as the example "Proxy requests within another http server" as well - same behaviour

@taris
Copy link
Author

taris commented Dec 16, 2011

It does work, when you use http.request() directly and i replaced node-http-proxy with an own small proxy module for now.
Maybe someone finds a bug or points me to my failtrain :D

@taris
Copy link
Author

taris commented Dec 26, 2011

did anybody at least verified this already?

@taris
Copy link
Author

taris commented Dec 28, 2011

Thanks to AvianFlu i got the right way. The clue is, that the request object is a stream and you need to handle it right away or buffer it. In this case the following code works:

var http = require('http'),
    httpProxy = require('http-proxy'),
    fs = require('fs');

//
// Create a proxy server with custom application logic
//
httpProxy.createServer(function (req, res, proxy) {
  //
  // Put your custom server logic here
  //
  console.log("##### PROXY: NEW REQUEST #####");

  //
  // Buffer the request so that `data` and `end` events
  // are not lost during async operation(s).
  //
  var buffer = httpProxy.buffer(req);

  /**
   * In a callback function the target server never receives a request and client keeps on "pending"
   */
  fs.readdir(__dirname, function(err, files)
  {
    console.dir(proxy);
    proxy.proxyRequest(req, res, {
        host: 'localhost',
        port: 9000,
        buffer: buffer
    });
  });
}).listen(8000);

http.createServer(function (req, res) {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.write('request successfully proxied: ' + req.url +'\n' + JSON.stringify(req.headers, true, 2));
  res.end();

  console.log("##### TARGET: REQUEST RECEIVED #####");
}).listen(9000);

console.log("---\nProxy listens on port 8000 and routes anything to port 9000");

Take a look at lines 18 and 29. You need to create the buffer, hand it over to the proxy and then start to route the request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant