版本:
目标: "/map_services/v1/getVersion" -> "http://localhost:8810/v1/getVersion"
问题: 1. /map_services 确实被代理了,但后端服务器显示请求的是"http://localhost:8810/map_services/v1/getVersion" 即 PathRewrite 并没有生效 2. 我发起的是 POST 请求,但后端服务器显示收到的是 GET 请求
export const getVersion = async (): Promise<string> => {
return await http("/map_services/v1/getVersion", {
method: HTTP_METHODS.POST,
data: {}
}).then((response) => {
if (response.data !== "") {
return response.data;
}
return "null";
})
}
const http = async (url: string,
{data, headers, ...customConfig}: IConfig = {})=>{
数据处理就不写了
···
return await window.fetch(url, config).then(async (res) => {
let dataRes: IRemoteResponse = await res.json();
if (dataRes.info !== SUCCESS_FLAG) {
return Promise.reject({message: "Bad Request!"});
}
if (dataRes.info) {
return dataRes;
} else {
return Promise.reject(dataRes);
}
});
}
const {createProxyMiddleware} = require('http-proxy-middleware');
module.exports = function (app) {
app.use(
'/map_services',
createProxyMiddleware({
target: 'http://localhost:8810',
PathRewrite: {
'^/map_services': '/'
}
})
);
}
1
abelyao 2023-03-15 17:01:19 +08:00
试试 target: 'http://localhost:8810/'
以 / 结尾,然后去掉 PathRewrite |
2
wzdsfl 2023-03-15 17:03:37 +08:00
我在项目里是这么设置的
``` '/api/example': { target: 'http://example.com', changeOrigin: true, rewrite: (path) => path.replace(/^\/api\/example/, '') }, ``` |
3
7anshuai 2023-03-15 18:42:35 +08:00 1
PathRewrite -> pathRewrite ,大小写问题?
|
4
Gav1nw OP 看了一下真的是!!!太感谢了
|