最近工作中遇到了个问题,一个老项目后端接口没有规范,接口开头不一致,是这种类型
/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',
});
最终是选用了下面这种方式