Location>code7788 >text

vite Setting up a Web Proxy

Popularity:609 ℃/2024-10-24 16:30:09

Refer to the documentation:vite official websitenode-http-proxy

Full Example:

export default defineConfig({
  server: {
    proxy: {
      // abbreviated way of writing a character string
      '/foo': 'http://localhost:4567',
      // Option writing
      '/api': {
        target: '',
        changeOrigin: true,
        rewrite: (path) => (/^\/api/, '')
      },
      // regular expression writing
      '^/fallback/.*': {
        target: '',
        changeOrigin: true,
        rewrite: (path) => (/^\/fallback/, '')
      },
      // utilization proxy an actual example
      '/api': {
        target: '',
        changeOrigin: true,
        configure: (proxy, options) => {
          // proxy be 'http-proxy' 的an actual example
        }
      }
    }
  }
})

Property Description:

  1. target: is the destination address, example'/foo': 'http://abc:4567' In the case of a complete requesthttp://loaclhost:8080/foo/login Proxy to address after matching keyword /foohttp://abc:4567/foo/login On, it should be noted that the replacement of the content in front of the keywords, keywords including the content behind will be added to the target address as is.
  2. changeOrigin: true/false, default: false, changes the source of the request header to the target's URL
  3. secure: true/false, whether to validate the https ssl certificate or not
  4. rewrite: Rewrite Address is a function that takes an address, modifies it, and returns it.