Unable to set cookies / write headers from proxy #445

robertjchristian opened this Issue Jul 28, 2013 · 6 comments


None yet

6 participants


I am trying to write a header on the response object from within the proxy:

// dependencies
var httpProxy = require('./lib/node-http-proxy'),
    http = require('http');

// Setup proxy server on 8000

var server = httpProxy.createServer(function (req, res, proxy) {

  // fetch cookies
  var cookies = {};
  req.headers.cookie && req.headers.cookie.split(';').forEach(function( cookie ) {
    var parts = cookie.split('=');
    cookies[ parts[ 0 ].trim() ] = ( parts[ 1 ] || '' ).trim();

  // write session cookie
  res.writeHead(200, {
    'Set-Cookie': 'token=12345678'    

  // proxy requests to localhost:9000
  proxy.proxyRequest(req, res, {
    host: 'localhost',
    port: 9000,


// Dummy target server on port 9000 (echo request)
http.createServer(function (req, res) {
 res.write('Echo service: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));

And http fails with "Cannot write header/s after they have been written." Reverse proxy documentation says nothing about setting cookies. What am I doing wrong?


This isn't going to work because proxy.proxyRequest calls res.writeHead too (and res.writeHead flushes headers). I don't think there's a way to walk this around so I'm going to leave this open as a feature request.

aiboy commented Aug 3, 2013

I suggest to close this issue since we can quite easily achieve replacing of headers by "emulating" express middlewere and replacing setHeader.

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

httpProxy.createServer(function(req, res, next){

    //this code will replace all set-cookie headers to ONE header that u want
    //if there will be 2 set-cookie headers
    //result will be 1 set-cookie:a=b header
    var _setHeader = res.setHeader.bind(res);
    res.setHeader = function(name, value) {
        if (name && name.toLowerCase() == "set-cookie") {
            return _setHeader("set-cookie", "a=b");
        } else {
            return _setHeader(name, value);

},function (req, res, proxy) {

    //usuall code
    proxy.proxyRequest(req, res, {
        host: 'someserver.com',
        port: 80

adi518 commented Apr 24, 2016 edited

@aiboy, I tried your solution on a recent boilerplate (since that was written in 2013) and while I'm able to modify/override any header property, I can't get a cookie to save under resources, jessionid in particular.

afilp commented Jun 9, 2016 edited

Is there any solution with cookies received from a proxied subdomain, which also contain the "domain" attribute?

We cannot authenticate from localhost (we are using webpack-dev-server) because of this problem. The 'domain' attribute for the received cookie is different from 'localhost' (obviously) and this causes the authentication to not work on the next call.

Is there any solution or trick to this problem? (i.e. automatically changing the "domain" attribute value through a proxy rule or code?)


afilp commented Jun 9, 2016

Actually... is this PR what I am really looking for?


If yes, can you please add it to the library?


qbaty commented Jul 13, 2016 edited

What status is this feature ? is this still going on?

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