1

So, I am proxying my API requests through a node-http-proxy for several reasons.

The external API has a different origin than the actual client, so cookies are not being set correctly. The proxy obviously runs at the same origin, so I want to receive the response from the API, and inside the proxy, change the cookie value to reflect the proper origin.

Here's my current setup:

// Proxy to API server
app.use('/api', (req, res) => {
  proxy.web(req, res, { target: targetUrl })
})

proxy.on('proxyRes', function (proxyRes, req, res) {
  console.log('RAW Response from the target', JSON.stringify(proxyRes.headers, true, 2))
  console.log('The original request', req.headers.host)
})

Basically, I need to modify the cookie to req.headers.host, as this is the correct origin.

I've seen Harmon, but this looks very involved and changes how you instantiate your entire app, if I understand correctly.

Is there a way to simply modify the proxyRes after receiving it, in a synchronous fashion?

It seems very strange that there is a proxyReq event that allows you to alter the proxy request before it's sent, but not an equivalent that allows you to alter the response...

j_d
  • 2,818
  • 9
  • 50
  • 91

1 Answers1

1

For anyone facing the same issue, I found a solution. They just merged a PR a few days ago that hasn't made it into a new release yet.

This PR introduces a new option called cookieDomainRewrite that does exactly what it sounds like. Simply include this in your config and it's all taken care of.

j_d
  • 2,818
  • 9
  • 50
  • 91