node-http-proxy - 正在修改代理的 HTTP 响应
问题描述
我有一个启动开发代理服务器的 gulpfile。
gulp.task('dumbserver', ()=> {
const express = require('express');
const httpProxy = require('http-proxy');
const app = express();
const proxy = httpProxy.createProxyServer();
app.use('/api', function (req, res) {
proxy.web(req, res, {target: 'https://bos1-vcd-sp-static-199-8.eng.vmware.com/api', secure: false},
(e) => console.log('error', e)
);
});
return app.listen(8080, function () {
console.log('Server started on port 8080.');
});
});
当我直接打电话给邮递员时,https://bos1-vcd-sp-static-199-8.eng.vmware.com/api/session
我得到以下有效负载:
<Session locationId="86171c79-e8f0-4c06-a0a8-4bc7fde76915@7bcf706c-d90f-4e1b-b0cc-b2a13db3e618" org="juan" roles="System Administrator" user="administrator" userId="urn:vcloud:user:7b5f0241-f597-4851-8cae-655a15afde24" href="https://bos1-vcd-sp-static-199-8.eng.vmware.com/api/session" type="application/vnd.vmware.vcloud.session+xml">
<Link rel="down" href="https://bos1-vcd-sp-static-199-8.eng.vmware.com/api/org/" type="application/vnd.vmware.vcloud.orgList+xml"/>
<Link rel="remove" href="https://bos1-vcd-sp-static-199-8.eng.vmware.com/api/session"/>
<Link rel="down" href="https://bos1-vcd-sp-static-199-8.eng.vmware.com/api/admin/" type="application/vnd.vmware.admin.vcloud+xml"/>
<Link rel="down" href="https://bos1-vcd-sp-static-199-8.eng.vmware.com/api/admin/extension" type="application/vnd.vmware.admin.vmwExtension+xml"/>
<Link rel="nsx" href="https://bos1-vcd-sp-static-199-8.eng.vmware.com/network" type="application/xml"/>
<Link rel="openapi" href="https://bos1-vcd-sp-static-199-8.eng.vmware.com/cloudapi" type="application/json"/>
</Session>
但是,当我使用 postman to 拨打电话时localhost:8080/api/session
,最后两个链接将 href 重写为
<Link rel="nsx" href="https://localhost:8080/network" type="application/xml"/>
<Link rel="openapi" href="https://localhost:8080/cloudapi" type="application/json"/>
但是其他链接都没有被重写。
关于可能发生的事情有什么建议吗?
解决方案
事实证明,它http-proxy
添加了一个HOST标头。生成链接的服务器端代码使用该HOST标头(如果可用)作为基本 URL,覆盖配置的值。
不删除问题是希望知道 http-proxy 向请求添加主机标头的事实可能会导致代码中的错误。
推荐阅读
- python - “numpy.float64”对象不能解释为整数
- rx-java - 每次从一个巨大的列表中发出 50 个项目
- vba - Excel VBA:在具有大量公式的工作簿中加载数据的有效方法
- google-apps-marketplace - G Suite Marketplace 上的屏幕截图未更新
- mysql - 在 Dockerfile 中创建动态用户 - MySQL
- c++ - Docker 容器中的 MXE Qt5 应用程序构建失败
- html - 将英雄图像插入一个部分
- cuda - 如何使用可视化分析器识别 CUDA 代码的带宽和失败率?
- angular - Angular - @Input 和 @Output 与可注入服务
- python-3.x - python中的AttributeError:对象没有属性