Vue3.x 中 vue.config 配置 Webpack-dev-server的proxy 实现代理跨域(接口调用的配置)
转载 高先生的猫
前言
- 如果你有单独的后端开发服务器 API,并且希望在同域名下发送 API 请求 ,那么代理某些 URL 会很有用。
- 解决开发环境的跨域问题(不用在去配置nginx和host, 爽歪歪~~
- 代理只能做在开发环境使用,也就是说生产环境还是需要后台处理好跨域,并且生产的接口域名也需要配置,默认是当前域名下(不同环境的接口请求域名配置可参考我的其他篇)
在webpack.config.js
中配置
下面简单介绍一下五个经常使用的场景
使用一:
1 2 3 4 5 6 7 8 | module.exports = { //... devServer: { proxy: { '/api' : 'http://localhost:3000' } } }; |
请求到 /api/xxx
现在会被代理到请求 http://localhost:3000/api/xxx
, 例如 /api/user
现在会被代理到请求 http://localhost:3000/api/user
使用二
如果你想要代码多个路径代理到同一个target下, 你可以使用由一个或多个「具有 context 属性的对象」构成的数组:
1 2 3 4 5 6 7 8 9 | module.exports = { //... devServer: { proxy: [{ context: [ '/auth' , '/api' ], target: 'http://localhost:3000' , }] } }; |
使用三:
如果你不想始终传递 /api ,则需要重写路径:
1 2 3 4 5 6 7 8 9 10 11 | module.exports = { //... devServer: { proxy: { '/api' : { target: 'http://localhost:3000' , pathRewrite: { '^/api' : '' } } } } }; |
请求到 /api/xxx 现在会被代理到请求 http://localhost:3000/xxx
, 例如 /api/user 现在会被代理到请求 http://localhost:3000/user
使用四:
默认情况下,不接受运行在 HTTPS 上,且使用了无效证书的后端服务器。如果你想要接受,只要设置 secure: false
就行。修改配置如下:
1 2 3 4 5 6 7 8 9 10 11 | module.exports = { //... devServer: { proxy: { '/api' : { target: 'https://other-server.example.com' , secure: false } } } }; |
使用五:
有时你不想代理所有的请求。可以基于一个函数的返回值绕过代理。
在函数中你可以访问请求体、响应体和代理选项。必须返回 false 或路径,来跳过代理请求。
例如:对于浏览器请求,你想要提供一个 HTML 页面,但是对于 API 请求则保持代理。你可以这样做:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | module.exports = { //... devServer: { proxy: { '/api' : { target: 'http://localhost:3000' , bypass: function (req, res, proxyOptions) { if (req.headers.accept.indexOf( 'html' ) !== -1) { console.log( 'Skipping proxy for browser request.' ); return '/index.html' ; } } } } } }; |
解决跨域原理
上面的参数列表中有一个changeOrigin
参数, 是一个布尔值, 设置为true, 本地就会虚拟一个服务器接收你的请求并代你发送该请求,
1 2 3 4 5 6 7 8 9 10 11 | module.exports = { //... devServer: { proxy: { '/api' : { target: 'http://localhost:3000' , changeOrigin: true , } } } }; |
vue-cli中proxyTable配置接口地址代理示例
修改 config/index.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | module.exports = { dev: { // 静态资源文件夹 assetsSubDirectory: 'static' , // 发布路径 assetsPublicPath: '/' , // 代理配置表,在这里可以配置特定的请求代理到对应的API接口 // 使用方法:https://vuejs-templates.github.io/webpack/proxy.html proxyTable: { // 例如将'localhost:8080/api/xxx'代理到'https://wangyaxing.cn/api/xxx' '/api' : { target: 'https://wangyaxing.cn' , // 接口的域名 secure: false , // 如果是https接口,需要配置这个参数 changeOrigin: true , // 如果接口跨域,需要进行这个参数配置 }, // 例如将'localhost:8080/img/xxx'代理到'https://cdn.wangyaxing.cn/xxx' '/img' : { target: 'https://cdn.wangyaxing.cn' , // 接口的域名 secure: false , // 如果是https接口,需要配置这个参数 changeOrigin: true , // 如果接口跨域,需要进行这个参数配置 pathRewrite: { '^/img' : '' } // pathRewrite 来重写地址,将前缀 '/api' 转为 '/'。 } }, // Various Dev Server settings host: 'localhost' , // can be overwritten by process.env.HOST port: 4200, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类