当前位置: 首页 > news >正文

Vue项目proxyTable跨域配置

Vue项目proxyTable跨域配置

  • 文章说明
  • proxyTable跨域配置
    • config / dev.env.js
    • config / prod.env.js
    • config / index.js
    • utils / request.js
    • 接口api.js
    • 路径转换解析

文章说明

学习连接 - 重要❤ - 一文详解vue-cli2.0与vue-cli3.0之间的区别


1. vue cli 2.0项目2. 本地运行时,不同端口号,前后端项目数据互通 通过proxyTable,所以只在index.js 里面的 dev配置了

proxyTable跨域配置

config / dev.env.js

'use strict'
const merge = require('webpack-merge')
const prodEnv = require('./prod.env')module.exports = merge(prodEnv, {NODE_ENV: '"development"',API_ROOT: '"http://192.168.2.121:8088/api"'
})

config / prod.env.js

'use strict'
module.exports = {NODE_ENV: '"production"',API_ROOT: '"https://www.***.com/api"'  // https://线上域名/api
}

config / index.js

'use strict'const path = require('path')
//const url = 'https://线上域名/api/'; //正式接口服务器路径
const url = 'http://127.0.0.1:8002';  //本地访问后端路径module.exports = {dev: {// PathsassetsSubDirectory: 'static',assetsPublicPath: '/',proxyTable: {'/api': {target: url,changeOrigin: true, // 跨域pathRewrite: {'^/api': '/' //把路径里面的api去掉}}},// Various Dev Server settingshost: '192.168.2.121', // can be overwritten by process.env.HOSTport: 8088, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined... ...}build: {// Template for index.htmlindex: path.resolve(__dirname, '../dist/index.html'),// PathsassetsRoot: path.resolve(__dirname, '../dist'),assetsSubDirectory: 'static',assetsPublicPath: '/',... ...}

utils / request.js


//新建 axios 实例 -- https://www.axios-http.cn/docs/intro
const request = axios.create({baseURL: process.env.API_ROOT, //前端路径,会通过 index.js 的 proxyTable 跨域处理timeout: 100000, //请求超时withCredentials: true, // 跨域请求,允许保存cookie
})... ...

接口api.js

import request from "./request";export function getCaptcha (){return request({url: '/captcha',method: 'get',responseType: 'arraybuffer'})
}

路径转换解析

本地访问页面:http://127.0.0.1:8088/index
后端访问接口:http://127.0.0.1:8002/captcha
直接从前端访问后端,数据不通,通过proxyTable进行跨域配置'/api': {target: url,这个含义就是 用  ' /api ' 区分访问 页面 还是 接口数据 http://127.0.0.1:8088/api ====就是访问target====> http://127.0.0.1:8002/apiproxyTable: {'/api': {target: url,changeOrigin: true, // 跨域,重要pathRewrite: {'^/api': '/' //把路径里面的api去掉}
}pathRewrite 作用路径重写,因为后端接口路劲没有 ' /api ' 这个字符串http://127.0.0.1:8088/api/captcha ====就是访问target====>   http://127.0.0.1:8002/api/captcha ====通过pathRewrite====>  http://127.0.0.1:8002/captcha
http://www.lryc.cn/news/45068.html

相关文章:

  • ubuntu16.04搭建gitlab
  • SSMP综合案例
  • 让你的作品更出色——词云Word Cloud的制作方法(基于python,WordCloud,stylecloud)
  • axios请求拦截器
  • 四个常见的Linux技术面问题
  • 有什么适合程序员查资料的网站
  • (七)手把手带你搭建精美简洁的个人时间管理网站—实现登录与注册的前端代码【源码】
  • Day933.如何将设计最终落地到代码 -系统重构实战
  • 209. 长度最小的子数组
  • 【数据结构与算法】查找(Search)【详解】
  • 一文学会 Spring MVC 表单标签
  • 如何在 Windows10 下运行 Tensorflow 的目标检测?
  • 【jvm系列-04】精通运行时数据区共享区域---堆
  • ctfshow愚人杯 re easy_pyc wp
  • Ubuntu18.04 系统中本地代码上传至Gitlab库
  • Leetcode.1665 完成所有任务的最少初始能量
  • 【C++笔试强训】第一天
  • 【网络安全软件】上海道宁与Cybereason为您提供未雨绸缪的攻击保护,终结对端点、整个企业以及网络上任何角落的网络攻击
  • 基于RK3568的Android11 适配 MIPI 屏幕
  • Ubuntu安装python
  • django 运用pycharm的各种故障汇总(1)
  • 【设计模式】单例模式Singleton(Java)
  • 机器学习中的公平性
  • Docker镜像之Docker Compose讲解
  • 蓝桥杯30天真题冲刺|题解报告|第三十天
  • 配置 Git Husky 代码提交约束
  • IntelliJ IDEA 2023.1 最新变化
  • stm32学习笔记-9 USART串口
  • 【蓝桥杯】每日四道编程题(两道真题+两道模拟)| 第四天
  • 大家有没有时候觉得,递归,分治,回溯,傻傻分不清楚?