Vite proxy doesnt work with api request to a local server running in docker

Viewed 114

My app is running locally on a vite dev server. I want to proxy my api requests to another local server running in docker, but approaches described in documentation don't help. When trying to request api, I get cors error

Access to XMLHttpRequest at 'http://localhost:8081/api/v1/configurations' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

vite configuration:

export default defineConfig({
  plugins: [svgr(), react()],
  define: {
    APP_VERSION: JSON.stringify(process.env.npm_package_version),
  },
  server: {
    proxy: {
      '/api': {
        target: "http://localhost:8081",
        changeOrigin: true,
        secure: false,
        rewrite: path => path.replace('/api', ''),
      }
    }
  },
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src'),
    },
  },
})

api request:

import axios from 'axios'

export const getConfigs = async () => {
  try {
    console.log(await axios.get("http://localhost:8081/api/v1/configurations"))
  } catch (e) {
    console.log(e)
  }
}
0 Answers
Related