[英]Change response cookies with node-http-proxy?
So, I am proxying my API requests through a node-http-proxy for several reasons. 因此,出于多种原因,我正在通过node-http-proxy代理我的API请求。
The external API has a different origin than the actual client, so cookies are not being set correctly. 外部API与实际客户端的来源不同,因此cookie设置不正确。 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. 代理服务器显然在同一来源运行,因此我想从API接收响应,并且在代理服务器内部,更改cookie值以反映正确的来源。
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. 基本上,我需要将cookie修改为req.headers.host
,因为这是正确的来源。
I've seen Harmon , but this looks very involved and changes how you instantiate your entire app, if I understand correctly. 我看过Harmon ,但是如果我理解正确的话,这看起来非常复杂 ,并且会改变您实例化整个应用程序的方式。
Is there a way to simply modify the proxyRes after receiving it, in a synchronous fashion? 有没有一种方法可以在接收到proxyRes 之后以同步方式简单地对其进行修改?
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... 似乎有一个proxyReq
事件让您在发送之前更改代理请求,但没有一个等效事件使您能够更改响应,这似乎很奇怪。
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. 他们几天前刚刚合并了一个PR ,但尚未将其发布到新版本中。
This PR introduces a new option called cookieDomainRewrite
that does exactly what it sounds like. 此PR引入了一个称为cookieDomainRewrite
的新选项,它的功能完全像听起来那样。 Simply include this in your config and it's all taken care of. 只需将其包含在您的配置中,就可以了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.