我正在尝试将 BrowserSync 配置为在服务器模式下工作,并将我的 API 请求代理到在同一计算机上不同端口上运行的后端,使用http 代理中间件 https://github.com/chimurai/http-proxy-middleware。我使用 Gulp 启动 BrowserSync。
BrowserSync 在端口 8081 上运行。我的后端在 8080 上运行。
这是我创建代理中间件的方法:
var proxyApi = proxy('/api', {target : 'http://localhost:8080/api', logLevel : 'debug'});
以下是我如何从 Gulp 任务运行 BrowserSync:
// Init BrowserSync with proxies as middleware and based on the dest dir
browserSync.init({
open: true,
port: config.proxyPort,
server: {
baseDir: config.destDir,
middleware: [proxyApi]
},
browser: "google chrome"
});
输出 :
[HPM] Proxy created: /api -> http://localhost:8080/api
一切看起来都不错。
但是当我打例如http://localhost:8081/api/users
,输出为:
[HPM] GET /api/users/123 -> http://localhost:8080/api
...我的客户收到 404 错误,因为/api
其本身与后端的任何内容都不匹配。
根据我对文档和示例的理解,目标实际上应该是http://localhost:8080/api/users/123
为什么路径的其余部分(在本例中/users/123
)被排除在外?
使用以下版本:
"gulp": "3.9.1",
"browser-sync": "2.16.0",
"http-proxy-middleware": "0.17.1",