I have a project with a frontend built in React and a backend using Node.js, both deployed separately. While everything works when navigating the app, I run into 404 errors upon refreshing any page.
Here’s an overview of my setup:
- Frontend: React application using Vite for building
- Backend: API server developed with Express.js
- Routing: Implemented through react-router-dom for client-side navigation
- Both components are hosted separately on a web service.
Below is my basic Vite configuration:
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
export default defineConfig({
server: {
proxy: {
'/api': {
target: 'https://my-api-server.example.com',
changeOrigin: true
}
}
},
plugins: [react()]
});
And this is how my Express server is set up:
import express from 'express';
import cors from 'cors';
import helmet from 'helmet';
import userRoutes from './routes/users.js';
const app = express();
app.use(cors());
app.use(express.json());
app.use(helmet());
app.use('/api', userRoutes);
if (process.env.NODE_ENV === 'production') {
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, '../frontend/build/index.html'));
});
}
app.listen(process.env.PORT || 5000);
I suspect that the problem lies in the fallback routing, but I can’t quite pinpoint the issue. Any suggestions on how to resolve this?