vue中针对后端接口不统一不规范的代理配置

16 篇文章 0 订阅
7 篇文章 0 订阅

最近工作中遇到了个问题,一个老项目后端接口没有规范,接口开头不一致,是这种类型

/ns-**

如果一个一个去配置那是相当费事,全局有很多,多方查找资料后找到了要给很好的方案,
可以使用正则匹配,比如

//path matching

createProxyMiddleware({...}) - matches any path, all requests will be proxied.
createProxyMiddleware('/', {...}) - matches any path, all requests will be proxied.
createProxyMiddleware('/api', {...}) - matches paths starting with /api
//multiple path matching

createProxyMiddleware(['/api', '/ajax', '/someotherpath'], {...})
//wildcard path matching

//For fine-grained control you can use wildcard matching. Glob pattern matching is done by micromatch. Visit micromatch or glob for more globbing examples.

createProxyMiddleware('**', {...}) matches any path, all requests will be proxied.
createProxyMiddleware('**/*.html', {...}) matches any path which ends with .html
createProxyMiddleware('/*.html', {...}) matches paths directly under path-absolute
createProxyMiddleware('/api/**/*.html', {...}) matches requests ending with .html in the path of /api
createProxyMiddleware(['/api/**', '/ajax/**'], {...}) combine multiple patterns
createProxyMiddleware(['/api/**', '!**/bad.json'], {...}) exclusion

还可以下面这种方式去处理

//custom matching

For full control you can provide a custom function to determine which requests should be proxied or not.

/**
 * @return {Boolean}
 */
const filter = function (pathname, req) {
  return pathname.match('^/api') && req.method === 'GET';
};

const apiProxy = createProxyMiddleware(filter, {
  target: 'http://www.example.org',
});

最终是选用了下面这种方式

具体可以参考这篇文档

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值