proxy代理

    技术2022-07-11  99

    后台未设置允许跨域请求时(侧重路径问题处理) 在与package.json文件的同级目录下创建 vue.config.js, 内容如下:

    参考地址:

    https://www.jianshu.com/p/058a4dbddac7https://segmentfault.com/a/1190000014492336 module.exports = { devServer: { publicPath: process.env.NODE_ENV === 'development' ? '/' : './', proxy: { '/api': { target: '<url>', pathRewrite: { '^/api' : '/api' } }, '/foo': { target: '<other_url>' pathRewrite: { '^/foo': '/foo' } } } } } /* 示例: 远程服务器未处理跨域,接口为: http://localhost:3000/api/ 本地开发环境中的url为: http://localhost:8080 1. proxy: { 2. '/api5': { 3. target: 'http://localhost:3000', 4. pathRewrite: { 5. '^/api5' : '/api' 6. } 7. } 8. } 其中 第2行中的 '/api5'是自定义的本地请求时的名字 第3行的target表示代理的服务器url 第4行的pathRewrite表示路径重写 第5行的'^/api5'是一个正则表达式,表示要匹配请求的url中,全部'http://localhost:8080/api5' 转接为 http://localhost:3000/api/ */
    Processed: 0.008, SQL: 9