CORS errors when serving Traccar Web static build via Express proxy to remote backend

Question:
What is the recommended way to serve the built Traccar Web app with a remote Traccar backend—without running the backend locally—so that /api/* calls behave exactly as they do in Vite dev (one JSON response, no CORS or HTML redirects)? Is there a known Express/Nginx configuration that replicates Vite’s proxy behavior, or must the Traccar backend be run locally to avoid these issues?

I’m trying to host the “modern web” frontend of Traccar (built with React/Vite) on Heroku using a static dist/ folder and an Express server, but I keep running into CORS and HTML-UI redirect issues that never occur in development.

What I’ve done so far:

Development (works):

Run npm start with Vite’s dev server, proxying

server: {
  proxy: {
    '/api': 'http://<vps-ip>:8082',
    '/api/socket': 'ws://<vps-ip>:8082',
  }
}

All calls to /api/server, /api/session, etc. return JSON in one shot.

Production (failing):

Run npm run build to output dist/, serve it with Express + http-proxy-middleware

Proxy config in server.mjs:

// ...Express + proxy setup...
app.use('/api', createProxyMiddleware({ target: API_URL, changeOrigin: true }));
app.use('/api/socket', /* ws proxy */);
app.use(express.static('build'));
app.get(/.*/, (_, res) => res.sendFile('build/index.html'));

Problem: GET /api/server returns a 302 → /server/ HTML page, then the browser tries to fetch that and CORS-blocks it. No amount of pathRewrite, followRedirects, or header tweaks reproduces Vite’s behavior of silently following the redirect and returning JSON.

in Single line: Is it possible to host traccar-web just like android and ios manager apps standalone or it will only work with traccar-server locally.

Hi Anton,

Thanks for the tip—your proxy settings work perfectly in the Vite dev server, but I’m hitting a wall when I switch to production. After running:

npm run build

I tried serving the build folder both through my Express proxy (server.cjs) and with a simple static server (e.g. serve -s build), but API requests still aren’t being proxied correctly (I end up with the same errors).

Screenshot 2025-05-31 at 04.29.34.png