How do you serve a React build from Express?

MERN: connecting React and the API · Freshers and experienced

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