Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

proxy does not work within a callback function #168

Closed
taris opened this Issue · 4 comments

1 participant

@taris

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

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

@taris

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

did anybody at least verified this already?

@taris

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.

@taris taris closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.