vue 开发踩过的坑,devServer proxy配置通配符,报错SyntaxError: Invalid regular expression: /**/*.html/: Nothing to repeat

官网文档:

proxy('**', {...}) 匹配所有路径, 所有请求都会被代理.
proxy('**/*.html', {...}) 匹配所有以html结尾的路径,
proxy('/*.html', {...}) 匹配所有/目录下的html结尾的路径
proxy('/api/**/*.html', {...}) **匹配所有/api目录下的任意子目录里的html结尾的路径**
proxy(['/api/**', '/ajax/**'], {...}) **匹配所有/api目录下的任意子目录的路径**
proxy(['/api/**', '!**/bad.json'], {...}) 

环境:vue/cli4

vue.config.js配置

module.exports={
    publicPath:'/dist/',
    devServer:{
        open: true, //是否自动弹出浏览器页面
        host: "localhost", 
        port: '8080',
        https: false,
        hotOnly: false, 
        proxy: {
            '**/*.html': {
                target: 'http://www.baidu.com', //API服务器的地址
                changeOrigin: true
            }
        }
    }
}

报错:

SyntaxError: Invalid regular expression: /**/*.html/: Nothing to repeat
    at String.match (<anonymous>)
    at context (F:\VueWorkSpace\vue-element\node_modules\@vue\cli-service\lib\util\prepareProxy.js:75:27)
    at Object.matchContext [as match] (F:\VueWorkSpace\vue-element\node_modules\http-proxy-middleware\lib\context-matcher.js:37:12)
    at shouldProxy (F:\VueWorkSpace\vue-element\node_modules\http-proxy-middleware\lib\index.js:87:27)
    at middleware (F:\VueWorkSpace\vue-element\node_modules\http-proxy-middleware\lib\index.js:44:9)
    at F:\VueWorkSpace\vue-element\node_modules\webpack-dev-server\lib\Server.js:317:18
    at Layer.handle [as handle_request] (F:\VueWorkSpace\vue-element\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (F:\VueWorkSpace\vue-element\node_modules\express\lib\router\index.js:317:13)
    at F:\VueWorkSpace\vue-element\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (F:\VueWorkSpace\vue-element\node_modules\express\lib\router\index.js:335:12)
    at next (F:\VueWorkSpace\vue-element\node_modules\express\lib\router\index.js:275:10)
    at goNext (F:\VueWorkSpace\vue-element\node_modules\webpack-dev-middleware\lib\middleware.js:28:16)
    at processRequest (F:\VueWorkSpace\vue-element\node_modules\webpack-dev-middleware\lib\middleware.js:92:26)
    at ready (F:\VueWorkSpace\vue-element\node_modules\webpack-dev-middleware\lib\util.js:53:12)
    at handleRequest (F:\VueWorkSpace\vue-element\node_modules\webpack-dev-middleware\lib\util.js:182:5)
    at F:\VueWorkSpace\vue-element\node_modules\webpack-dev-middleware\lib\middleware.js:64:7

报错代码:return pathname.match(context)
解决办法
使用js 标准正则

Logo

前往低代码交流专区

更多推荐