Loading post…
Loading post…
11 Sept 20261 min read
The folder layout, boundaries and habits that keep a MERN codebase clean past month three.
Everything lives in one server.js for a week. Then routes multiply, models drift, and auth logic ends up copy-pasted in six places. Here is the shape I use instead:
server/
config/ env, db connection, constants
models/ Mongoose schemas only, no logic
routes/ thin HTTP layer: validate, call service, respond
services/ business logic lives here and only here
middleware/ auth, errors, rate limits
utils/ small pure helpers
if (!req.user) inside a route, move it out.| Smell | Fix |
|---|---|
| Route file over 100 lines | Extract a service |
| Same query twice | Share it via the service layer |
| Auth checks inline | Middleware |
Boring structure beats clever structure. Future you is already grateful.
Links