How to Structure a Node.js Project Folder: A Practical Layout
A clean folder structure prevents spaghetti code. Here is a practical Node.js folder layout.
How to Structure a Node.js Project Folder
Folder structure is a small choice with a big payoff. A clean structure keeps code findable and the codebase maintainable.
The Layout
src/
config/
index.js
middlewares/
auth.js
error.js
validate.js
models/
user.js
connection.js
message.js
routes/
auth.js
user.js
feed.js
chat.js
controllers/
authController.js
userController.js
feedController.js
chatController.js
utils/
db.js
logger.js
email.js
app.js
server.js
.env
What Each Folder Does
- config/: env vars, app-level setup, database connection.
- routes/: declares endpoints and binds them to controllers.
- controllers/: handles request/response and calls models.
- models/: Mongoose schemas and models.
- middlewares/: auth, error, validation middlewares.
- utils/: shared helpers like logger, email, db connection.
Why This Works
Separation of concerns. Routes declare endpoints; controllers handle logic; models shape data; middlewares handle cross-cutting concerns. Each file has one job.
Keep Controllers Thin
Controllers should not contain business logic. Move complex logic to a services folder if needed. Controllers parse the request, call the service, and send the response.
Keep Models Clean
Models define the schema and validation. Do not put route logic in models. Models are the data shape, not the data flow.
Single Responsibility
One file, one concern. userController.js handles user requests. It does not import from feedController.js. If two controllers share logic, move that logic to utils or services.
The Takeaway
A clean Node.js folder structure separates config, routes, controllers, models, middlewares, and utils. Each file has one job. This keeps the codebase findable and maintainable as it grows.
Separate folders for config, routes, controllers, models, middlewares, and utils. Each file has one job. This is separation of concerns and keeps the codebase maintainable.
Routes declare the endpoints and bind them to controllers. Controllers handle the request/response logic and call models. Routes are the map; controllers are the handlers.
In a utils folder. Things like the database connection, logger, email sender, and other helpers that multiple modules use live in utils.
Keep controllers thin. Put complex logic in a services folder if needed. Controllers should parse the request, call the service, and send the response.
To centralize env vars, database connection, and app-level setup. Keeping config separate from logic prevents env values from being scattered across the codebase.
Ready to master Node.js completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master Node.js
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

