广告
返回顶部
首页 > 资讯 > 前端开发 > JavaScript >vue之proxyTable代理超全面配置流程
  • 101
分享到

vue之proxyTable代理超全面配置流程

2024-04-02 19:04:59 101人浏览 薄情痞子
摘要

目录前言介绍配置分离1. config.dev.js2. proxyTableHandler.js效果如下前言 用了Vue有一年多了,从最初的摸着石头过河到现在已经能熟练的使用vue

前言

用了Vue有一年多了,从最初的摸着石头过河到现在已经能熟练的使用vue开发项目,学到了许多,特别是vue的代理配置让我眼前一亮,甚是喜欢,故将自己对proxyTable代理配置整理出来,供致力于的开源的同辈浏览,望大家手下留情,哈哈_。

介绍

vue的proxyTable是用于开发阶段配置跨域的工具,可以同时配置多个后台服务器跨越请求接口,其真正依赖的npm包是Http-proxy-middleware,在GitHub上拥有更丰富的配置,按需配置咯。

配置分离

我将代理配置抽离出2个配置文件

在这里插入图片描述

1. config.dev.js

用于配置后端服务器地址、端口和IP等

2. proxyTableHandler.js

用于添加代理的配置项

config.dev.js如下


module.exports = {
   // 开发环境代理服务器
   devProxy: {
       host: '0.0.0.0', // ip/localhost都可以访问
       port: 8080
   },
   // 后端服务器地址
   servers: {
     default: 'http://localhost:8081/SpringBoot-girl',
     jsp: 'http://localhost:8082/springboot-jsp'
   }
}

proxyTableHandler.js如下


const configDev = require('../config.dev')
module.exports = (() => {
	let proxyapi = {}
    let servers = configDev.servers
    for (let key of Object.keys(servers)) {
        proxyApi[`/${key}`] = {
            // 传递给http(s)请求的对象
            target: servers[key],
            // 是否将主机头的源更改为目标URL
            changeOrigin: true,
            // 是否代理websocket
            ws: true,
            // 是否验证SSL证书
            secure: false,
            // 重写set-cookie标头的域,删除域名
            cookieDomainRewrite: '',
            // 代理响应事件
            onProxyRes: onProxyRes,
            // 重写目标的url路径
            pathRewrite: {
                [`^/${key}`]: ''
            }
        }
    }
    return proxyApi
})()

function onProxyRes (proxyRes, req, res) {
  let cookies = proxyRes.headers['set-cookie']
  // 目标路径
  let originalUrl = req.originalUrl
  // 代理路径名
  let proxyName = originalUrl.split('/')[1] || ''
  // 开发服url
  let server = configDev.servers[proxyName]
  // 后台工程名
  let projectName = server.substring(server.lastIndexOf('/') + 1)
  // 修改cookie Path
  if (cookies) {
      let newCookie = cookies.map(function (cookie) {
          if (cookie.indexOf(`Path=/${projectName}`) >= 0) {
              cookie = cookie.replace(`Path=/${projectName}`, 'Path=/')
              return cookie.replace(`Path=//`, 'Path=/')
          }
          return cookie
      })
      // 修改cookie path
      delete proxyRes.headers['set-cookie']
      proxyRes.headers['set-cookie'] = newCookie
  }
}

使用方式 config/index.js

const configDev = require('./config.dev')
module.exports = {
	dev: {
    // Paths
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',
    proxyTable: proxyTableHandler,
    // Various Dev Server settings
    host: configDev.devProxy.host, // can be overwritten by process.env.HOST
    port: configDev.devProxy.port, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined
    autoOpenBrowser: false,
    errorOverlay: true,
    notifyOnErrors: true,
    poll: false, // https://webpack.js.org/configuration/dev-server/#devserver-watchoptions-
    // Use Eslint Loader?
    // If true, your code will be linted during bundling and
    // linting errors and warnings will be shown in the console.
    useEslint: true,
    // If true, eslint errors and warnings will also be shown in the error overlay
    // in the browser.
    showEslintErrorsInOverlay: false,
    
    // https://WEBpack.js.org/configuration/devtool/#development
    devtool: 'cheap-module-eval-source-map',
    // If you have problems debugging vue-files in devtools,
    // set this to false - it *may* help
    // https://vue-loader.vuejs.org/en/options.html#cachebusting
    cacheBusting: true,
    CSSSourceMap: true
  }
}

效果如下

以/jsp开头的api

在这里插入图片描述

以/default开头的api

在这里插入图片描述

至此配置代理成功!

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。 

--结束END--

本文标题: vue之proxyTable代理超全面配置流程

本文链接: https://www.lsjlt.com/news/145946.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • vue之proxyTable代理超全面配置流程
    目录前言介绍配置分离1. config.dev.js2. proxyTableHandler.js效果如下前言 用了vue有一年多了,从最初的摸着石头过河到现在已经能熟练的使用vue...
    99+
    2022-11-13
  • vue之proxyTable代理全面配置的方法
    本篇内容介绍了“vue之proxyTable代理全面配置的方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!介绍vue的proxyTable...
    99+
    2023-06-30
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作