How do you serve a React build from Express?
Model answer
Run npm run build, then app.use(express.static(path.join(__dirname, '../client/dist'))) and add a catch-all that sends index.html for non-API routes so client-side routing works on refresh. Register API routes before the catch-all. Often nginx serves the static files instead for speed.
Learn this topic
useEffect and Fetching Data from Your API: Loading, Errors and Cleanup in the course MERN Stack Full Course: MongoDB, Express, React and Node.js.
Sign in to add your own answer.
More questions
- How does a React app call an Express API?
- How do you set up a Vite proxy for development?
- How do you handle authentication state in a MERN frontend?
- How do you handle loading, error and empty states for API data in React?
- How do you build a dashboard with filters and pagination backed by the API?
- How do you share validation and types between frontend and backend?