Express.js & REST APIs
Node.js Express Project Structure
A maintainable Express project separates routing, business logic and data access. Routes describe URLs, controllers coordinate, services hold the logic, and models talk to the database.
What is Express Project Structure in Node.js?
A maintainable Express project separates routing, business logic and data access. Routes describe URLs, controllers coordinate, services hold the logic, and models talk to the database.
Express Project Structure example
Output
src/
├── index.js # bootstrap: config, db, app.listen
├── app.js # express app, middleware, route mounting
├── config/ # env parsing, database connection
├── routes/ # jobs.routes.js — URLs only
├── controllers/ # jobs.controller.js — req/res handling
├── services/ # jobs.service.js — business logic, no req/res
├── models/ # jobs.model.js — schema and queries
├── middleware/ # auth, validate, errorHandler
└── utils/ # logger, helpersKey points to remember
- Services must not touch req or res — that keeps them testable and reusable.
- Separating app.js from index.js lets tests import the app without starting a server.
- Keep each layer thin; a controller that is fifty lines is doing service work.
Common mistakes with Express Project Structure
- Database queries written directly inside route handlers.
- Business logic in controllers, which makes it impossible to reuse from a job or CLI.
Node.js Express Project Structure— Interview Questions & FAQs
How should I structure a Node.js Express project?+
Layer it: routes define URLs, controllers handle request and response, services hold business logic, models handle data. Split app creation from server startup so tests can import the app directly.
