Stop 11 · Connecting React to ExpressTopics · 3/10

Vite Dev Proxy

Part of the MERN Stack Roadmap

In short

In development React runs on localhost:5173 and Express on localhost:5000. Two origins means CORS errors and hardcoded URLs. A few lines of Vite config make them feel like one app.

Vite's server.proxy setting forwards requests like /api from the React dev server to Express, so in development the browser sees one origin and you avoid CORS issues.

The jaadu in a guard's cap stands at an apartment society gate, taking a parcel from a delivery rider and passing it through to a building behind.

Think of it like this

The society security gate

Visitors to your apartment society only know the main gate address. When a courier arrives for Flat B-204, the guard at the gate forwards the parcel inside. The courier never needs the flat's internal route. Vite's proxy is that guard: the browser talks only to 5173, and anything addressed to /api is forwarded to Express.

Watch it happen

One request through the proxy

Browser
Vite :5173
Express :5000

GET /api/notes

1/4The browser calls a relative URL on the same origin it loaded the page from.

The big ideas

What you need to know

server.proxy in vite.config.js

Map a path prefix like /api to your Express URL. Any request starting with it is forwarded.

Use relative URLs

Call fetch("/api/notes"), not fetch("http://localhost:5000/api/notes"). The same code works in development and behind a single domain in production.

Dev only

The proxy exists only while npm run dev is running. In production you need the same domain, a reverse proxy, or CORS plus an env-based API URL.

Cookies just work

Because the browser sees one origin, httpOnly auth cookies are sent without extra CORS credential settings in development.

See it in code

Walk through the code

js
1// client/vite.config.js2import { defineConfig } from "vite";3import react from "@vitejs/plugin-react";4 5export default defineConfig({6  plugins: [react()],7  server: {8    port: 5173,9    proxy: {10      "/api": {11        target: "http://localhost:5000",12        changeOrigin: true,13      },14      "/socket.io": {15        target: "http://localhost:5000",16        ws: true,17      },18    },19  },20});21 22// client/src/api/notes.js23export async function getNotes() {24  const res = await fetch("/api/notes");25  if (!res.ok) throw new Error("Failed to load notes");26  return res.json();27}

1/4Dev server settings live under server.

Forward /api and Socket.IO traffic to Express while you develop.

Save yourself the pain

Common mistakes

Oops

Adding the proxy but still calling http://localhost:5000/api in fetch.

Do this instead

Use relative URLs like /api/notes. Absolute URLs skip the proxy and hit CORS again.

Oops

Expecting the proxy to work on Vercel or Netlify after deploying.

Do this instead

It is a dev server feature. Use rewrites, one domain, or VITE_API_URL with CORS in production.

Oops

Editing vite.config.js and not restarting the dev server.

Do this instead

Config changes need a restart of npm run dev.

Your mission

Kill the CORS error

~15 min

Quick check

Why does the Vite proxy avoid CORS errors in development?

Go deeper

  1. 1Add a proxy for /api to http://localhost:5000 in vite.config.js.
  2. 2Call fetch('/api/products') with a relative URL from React.
  3. 3Remember the proxy only exists in development, so production needs real configuration.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge