How is the routing done, is it with react router on the client side? An explanation about how routing is done in the mern stack would be awesome to read!
Asked
Active
Viewed 1,256 times
0
-
1This is a good question. I used to ask myself this too. How routing is handled can be puzzling because it's done on the client and the server. So the question is useful and deserves to be answered. Moreover it's clearly stated. I'm not sure why it's being voted down. It may be because there are other questions that have posed similar concerns. . . . – Luke May 04 '21 at 21:22
-
1Your question is a possible duplicate of this one: https://stackoverflow.com/questions/28553904/client-routing-using-react-router-and-server-side-routing?rq=1 This is why you may be seeing it downvoted and/or eventually closed. – Luke May 04 '21 at 21:37
1 Answers
0
With the MERN stack you use the client side routing to render different components or different groups of components. For example, you could have a route called "/admin" that renders an admin GUI built from your React components. But these components still aren't by default loaded with any data. They would get that data by making Javascript fetch requests to the server with calls like, for example GET "/api/users" or GET "/api/roles". These latter endpoints would be managed by your server side router.

Luke
- 2,751
- 1
- 17
- 20
-
The best way to really understand how MERN client side and server side routing work together is to build a small application that uses them. – Luke May 04 '21 at 21:31
-
In addition, unless you are hosting your React code on a completely different server, you would still need a server side endpoint to serve up your main React component. Typically, for example GET "/" would be a server side route that would return your main index.html page and your compiled React bundle which contains your React router. So both routers are needed. – Luke May 04 '21 at 21:34