How do I properly configure vite proxy?

Viewed 22

What I want to do is simple. Whenever there is a request to http://localhost/api/some/resource I want it to be rewritten to http://localhost:5001/atomic-swap-124d0/us-central1/main/api/some/resource.

This is my proxy config:

 server: {
      proxy: {
        "/api": {
          prependPath: true,
          target: "http://localhost:5001/atomic-swap-124d0/us-central1/main/",
        },
      },
    },
  vite:proxy /api/blockfrost/testnet/health -> http://localhost:5001/atomic-swap-124d0/us-central1/main/ +23s
22:16:00 [vite] http proxy error:
Error: connect ECONNREFUSED ::1:5001
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1300:16)
  vite:time 4.11ms /api/blockfrost/testnet/health +23s

What do I have to change to get the expected result?

1 Answers

pnpm add -D path-browserify;

import path from 'path-browserify';

'/api': {
      target: `http://localhost:5001`,
      changeOrigin: true,
      rewrite: path => path.replace(/^\/api/, '/atomic-swap-124d0/us-central1/main/api')
},
Related