vue devserver proxy设置跨域详解

你要请求的线上域名

http://baidu.com

你要请求的线上接口

/news/list

拼接起来就是这样的

http://baidu.com/news/list

然后vue.config.js配置是这样的:

复制代码
module.exports = { 
    devServer: {   
        proxy: {   
            '/api': {    //这里的/api表示的意思是以/api开头的才生效 ->刷下面的重点
                target: "http://baidu.com",
                changOrigin: true,   //如果接口跨域这里就要这个参数配置
                pathRewrite: {
                    //'^/api': '/api'  //实际请求地址是http://baidu.com/api/news/list
                    '^/api': '/'  //实际请求地址是http://baidu.com/news/list
                    // 我的理解就是http://baidu.com替换了/api/news/list里面的/api
                }
    }  
}

升级版:打印proxy代理地址:vue.config.js

复制代码
  devServer: {
    proxy:{
      '/dev':{
        target: "https://baidu.com",
        changeOrigin: true,
        secure:false,
        onProxyReq: function(proxyReq, req, res) {
          const realUrl = proxyReq.protocol + '//' + proxyReq.host + proxyReq.path
          console.log('Proxying real request url: ' + realUrl);
        }
      }
    },
    port: 8443,
    open: false,
    disableHostCheck: true,
  }
相关推荐
whinc1 天前
JavaScript技术周刊 2026年第18周
javascript
码海扬帆:前端探索之旅1 天前
深度定制 uni-combox:新增功能详解与实战指南
前端·vue.js·uni-app
谷雨不太卷1 天前
进程的状态码
java·前端·算法
打小就很皮...1 天前
基于 Python + LangChain + RAG 的知识检索系统实战
前端·langchain·embedding·rag
whinc1 天前
JavaScript技术周刊 2026年第17周
javascript
BJ-Giser1 天前
Cesium 烟雾粒子特效
前端·可视化·cesium
空中海1 天前
02 ArkTS 语言与工程规范
java·前端·spring