Skip to content
This repository

Accessing headers from proxy target response #236

Closed
jrolfs opened this Issue · 12 comments

8 participants

Jamie Rolfs Madhusudhan Srinivasa Christian Howe Josh Koenig Jonathan Crossman Travis Dunn Jay Shirley Charlie Robbins
Jamie Rolfs

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?

Madhusudhan Srinivasa

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

Jamie Rolfs

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.

Christian Howe

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.

Jamie Rolfs

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.

Josh Koenig

+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

Jamie Rolfs
jrolfs commented

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

Madhusudhan Srinivasa

+1

Travis Dunn

+1 and statusCode would be great as well.

Jay Shirley

+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

Jamie Rolfs
Charlie Robbins
Owner

Fixed in #356

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.