Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • N node-http-proxy
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 482
    • Issues 482
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 102
    • Merge requests 102
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • http ... PARTY!
  • node-http-proxy
  • Issues
  • #445
Closed
Open
Issue created Jul 28, 2013 by Administrator@rootContributor

Unable to set cookies / write headers from proxy

Created by: robertjchristian

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();
  });
  
  console.log(cookies);

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

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

});
server.listen(8000);



//
// 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));
  
  res.end();
  
}).listen(9000);

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?

Assignee
Assign to
Time tracking