Skip to content

Accessing headers from proxy target response #236

Closed
jrolfs opened this Issue Apr 24, 2012 · 12 comments

8 participants

@jrolfs
jrolfs commented Apr 24, 2012

I'm trying to access the headers that are passed back from the proxied server and I can't find a way to access them. I tried accessing the response object on the 'end' event emitted from the RoutingProxies internal HttpProxy:

var express = require('express'),
    proxy = require('http-proxy'),
    app = express.createServer();

var routingProxy = new proxy.RoutingProxy();
var host = 'my.other.server.com';
var port = 8080;


function apiProxy(pattern, host, port) {
    var hasListener = false;
    return function(req, res, next) {
        if (req.url.match(pattern)) {
            routingProxy.proxyRequest(req, res, {host: host, port: port});
            if (!hasListener) {     
                routingProxy.proxies[host + ':' + port].on('end', function (req, res) {
                    // Access headers here.
                });
            }
        } else {
            next();
        }
    }
}

app.configure(function () {
    // Proxy to api
    app.use(apiProxy(new RegExp('\/' + api + '\/.*'), host, port));

    // Serve static content
    app.use(express.methodOverride());
    app.use(express.bodyParser());
    app.use(express.static(__dirname));
    app.use(express.errorHandler({
        dumpExceptions: true, 
        showStack: true
    }));
    app.use(app.router);
});

app.listen(3000);

Is there no way to accomplish this currently or am I overlooking something?

@madhums
madhums commented Apr 25, 2012

I am having a similar issue, the cookies I am sending in response are not received when I am using the proxy.

@jrolfs
jrolfs commented Apr 25, 2012

I managed a hack of a solution for this. You'll need to use the my fork of node-http-proxy and something along the lines of this: https://gist.github.com/2490760. Keep in mind I am fairly new to Node.js and either way this server should not be used in production or anything critical as I have only done minimal testing and have just been using it as a development server.

@coderarity

The problem with response.getHeader is that the header has to be "queued but not sent to the client." You essentially need to get response.headers from inside the reverse proxy, but I don't think that's in the API at this time.

@jrolfs
jrolfs commented Apr 28, 2012

Yeah if you look at my fork, I exposed them. If that is a desired feature I would be happy to fully flesh it out and document it.

@joshkoenig

+1 for this - it'd be very useful for me to be able to read/write the response headers before returning them to the client

@jrolfs
jrolfs commented May 24, 2012

Awesome, there's some interest! haha. I'd be more than happy to discuss an ideal solution for exposing this stuff.

@crossman

👍

@madhums
madhums commented Jul 11, 2012

+1

@blahed
blahed commented Aug 3, 2012

+1 and statusCode would be great as well.

@jshirley

+1 but @jrolfs, I just installed from your fork and you have some conflict markers checked in.

https://github.com/jrolfs/node-http-proxy/blob/master/lib/node-http-proxy/http-proxy.js#L292

@jrolfs
@indexzero
nodejitsu member

Fixed in #356

@indexzero indexzero closed this Mar 9, 2013
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.