Repository navigation
React Frontend with Express: Server-Side Rendering or API-Based Architecture? #7492
|
For a React frontend, is it better to render the views through the Express server, or should the React frontend communicate with the server through an API? I'm working on a project with a React frontend and an Express/Node.js backend. I'm wondering which approach is generally better:
What are the main advantages and disadvantages of each approach, and which one would you recommend for a typical modern web application? |
Replies: 1 comment
|
I build exactly this setup (React + Vite frontend, Express + MongoDB backend), and for most apps I'd go with a separate frontend talking to an API. Why:
Trade-offs: you have to handle CORS (or proxy Rendering React through Express (SSR) is worth it mainly when SEO or first paint really matter, like public marketing pages, blogs or e-commerce. Hand-rolling SSR in Express ( Middle ground: keep the API approach, but have Express serve Vite's built For a typical app (dashboards, anything behind a login), I'd start with the API approach and only reach for SSR when a page actually needs it. |
I build exactly this setup (React + Vite frontend, Express + MongoDB backend), and for most apps I'd go with a separate frontend talking to an API.
Why:
Trade-offs: you have to handle CORS (or proxy
/apithrough Vite in dev) and auth across origins. In my own app I usejsonwebtokenfor user sessions: an auth middleware verifies the token, and if it's missing or invalid it responds withres.status(401)in one…