Skip to content

Accessing headers from proxy target response #236

jrolfs opened this Issue Apr 24, 2012 · 12 comments

8 participants

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 = '';
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 {

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

    // Serve static content
        dumpExceptions: true, 
        showStack: true


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

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


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 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.


+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 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.



madhums commented Jul 11, 2012


blahed commented Aug 3, 2012

+1 and statusCode would be great as well.


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

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.