How should I structure my MERN project #193161
-
🏷️ Discussion TypeQuestion BodyHi everyone, I'm currently building a MERN stack application and would like some guidance on structuring the project properly as it grows. What I'm working on: A full-stack web application using:
The app includes features like authentication, CRUD operations, and an admin dashboard. My current structure: Backend: /backend Frontend: /frontend My concerns:
What I'm looking for:
Question: What is the recommended way to structure a MERN project for better scalability and maintainability? Thanks in advance! Guidelines
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
For a MERN app that is expected to grow, I’d recommend moving from a pure layer-based structure ( That usually scales better because React encourages breaking the UI into reusable pieces and reusing logic through custom hooks, while Express is designed around routers and middleware that can be grouped by concern. ([React]1) A good rule is:
So instead of this: prefer something like this: And on the frontend: Why this works better:
For the backend, I’d also separate responsibilities clearly:
That keeps controllers thin and makes your code easier to test and maintain. Express supports composing logic cleanly with router-level and application-level middleware, so this style fits the framework well. ([Express.js]2) For the frontend, keep these guidelines:
React’s docs explicitly encourage composing apps from components and reusing logic through custom hooks, which maps well to a feature-first structure. React also emphasizes being intentional about state organization as apps grow. ([React]3) A few practical best practices:
If the app becomes much larger, you can evolve this into a modular monolith first before thinking about microservices. In most MERN apps, a modular monolith is the sweet spot for maintainability without unnecessary complexity. So the short answer is:
That gives you the best balance of clarity, scaling, and real-world maintainability. |
Beta Was this translation helpful? Give feedback.
For a MERN app that is expected to grow, I’d recommend moving from a pure layer-based structure (
controllers,routes,models, etc.) to a feature-based structure with shared infrastructure.That usually scales better because React encourages breaking the UI into reusable pieces and reusing logic through custom hooks, while Express is designed around routers and middleware that can be grouped by concern. ([React]1)
A good rule is:
auth,users,products,dashboardmiddleware,utils,config,lib,types,validatorsSo instead of this:
prefer something like this: