当前位置:网站首页>Vite configure proxy proxy to solve cross domain

Vite configure proxy proxy to solve cross domain

2022-04-23 17:46:00 Suk_ A Shuo

Code up :

import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import { resolve } from 'path';
// https://vitejs.dev/config/
export default defineConfig({
  server: {
    port: 9090,
    strictPort: true, //  Strict port  true: If the port is already in use , You just quit , Instead of trying on subsequent ports .
    /**
     * @description  solve chrome Set up origin:* Also cross domain mechanisms , agent /api Prefix to service base address 
     *  The final address will be axios Set up baseUrl:/api The agent is spliced into [target][/api], And then through rewrite Rewrite it /api by ''  This is our real base site 
     */
    proxy: {
      '/api': {
        target: 'http://www.baidu.com', //  Interface address base 
        rewrite: path => {
          console.log(path); //  Print [/api/userInfo]  This is it. http-proxy Requesting url, Our base address is actually not available /api  therefore replace fall 
          return path.replace(/^\/api/, '');
        }
      }
    }
  },
  plugins: [vue()],
  resolve: {
    alias: {
      '@': resolve(__dirname, '.', 'src') //  Set up  @  Point to  src
    }
  },
})

版权声明
本文为[Suk_ A Shuo]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231741180609.html