[英]http-proxy-middleware: pathFilter not working as expected
我正在嘗試使用兩台服務器設置基於 npm 的 HTML 開發環境。 One hosts my HTML app of static files (standard way: http://localhost:8080
) while the other exposes an application api running locally( http://127.0.0.1:57146/api/someEntity
). 后者是我無法控制的。 直接從我的應用程序調用 api URL 會引發 CORS 問題。 So i want to setup a proxy that redirects my calls from http://localhost:8080/apiBase/someEntity
to http://127.0.0.1:57146/api/someEntity
... hoping to avoid the CORS problems that way.
我正在使用lite-server ,它建立在Browsersync之上。
由於我的 static 應用程序文件不需要重定向,因此它們不應受到代理的影響。
要指定重定向哪些調用,我正在嘗試使用選項中的“pathFilter”字段。 但我無法讓它發揮作用。 相反,所有調用總是代理到http://127.0.0.1:57146
。 “pathRewrite”似乎也不起作用。 所以看起來我在這里缺少一些基本的東西。 我究竟做錯了什么?
這是我的配置文件:
//bs-config.cjs
const { createProxyMiddleware } = require('http-proxy-middleware');
const apiProxy = createProxyMiddleware({
target: "http://127.0.0.1:57146/",
pathFilter:"apiBase/",
pathRewrite: {
"apiBase/": "api/"
},
logger: "console",
logLevel: "debug"
});
module.exports = {
port: 8080,
index: "parent.htm",
startPath: "parent.htm",
cors: true,
server: {
baseDir: "./dist",
index: "parent.htm",
cors: true,
middleware: [ apiProxy ]
}
};
在(非常)耐心的后端人員的幫助下,我終於找到了解決方案。 我們無法讓“pathRewrite”工作,但在過濾器運行后(通過更改一些語法),調整路徑最終在工作設置中結束。
//bs-config.cjs
const { createProxyMiddleware } = require('http-proxy-middleware');
const proxy_filter = function (path, req) {
const regx = new RegExp("/apiBase");
return path.match(regx);
};
const proxy_options = {
target: "http://127.0.0.1:57146/api/",
changeOrigin: true,
logger: "console",
logLevel: "debug"
}
const apiProxy = createProxyMiddleware(proxy_filter, proxy_options);
module.exports = {
port: 8080,
index: "parent.htm",
startPath: "parent.htm",
server: {
baseDir: "./dist",
index: "parent.htm",
middleware: [
apiProxy
]
}
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.