
const onProxyRes = (proxy) => {
for (const api in proxy) {
const {target, ws, changeOrigin, pathRewrite} = proxy[api];
proxy[api] = {
target,
ws: ws || false,
changeOrigin: changeOrigin || false,
pathRewrite: pathRewrite || undefined,
onProxyRes(proxyRes, req) {
try {
const targetObj = new URL(target)
let reqUrl = req.url
if (pathRewrite) {
for (const key in pathRewrite) {
const replaceValue = pathRewrite[key];
let pattern = new RegExp(key)
reqUrl = req.url.replace(pattern, replaceValue)
}
}
let pathname = (targetObj.pathname + reqUrl).replace('//', '/')
const realUrl = new URL(pathname, targetObj.origin)?.href ?? ''
proxyRes.headers['x-real-url'] = realUrl
} catch (error) {
proxyRes.headers['x-real-url'] = 'undefined'
}
}
}
}
return proxy
}
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。
用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。