Structure, logging, testing and shutdown · mid
Project structure: routes, controllers, services
Keep HTTP details in thin routes and controllers, business rules in services, and database access in repositories, so each layer can be tested and changed without touching the others.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- Routes: path + middleware -> controller. Controllers: read req, call a service, map the result to a response. Services: business logic, no req/res. Repositories: SQL or ORM calls.
- Split app from server: app.ts builds and exports the Express app; server.ts calls listen. Tests import the app without opening a port.
- Config lives in one module and is validated at startup (for example zod over process.env). Fail fast if a required variable is missing.
- Services throw typed errors (NotFoundError, ConflictError); one error middleware maps them to HTTP.
- Group by feature (users/, orders/) once the app grows, rather than by type (controllers/, models/).
- Simple dependency injection: pass the repository into a service factory. Unit tests pass a fake.
The red pen
Common traps: mistake → fix
Mistake: Put all logic in route handlers, it is quicker
Fix: logic tied to req/res is hard to test and to reuse from jobs, CLIs or other routes.
Mistake: More layers are always better
Fix: a small service needs routes and services; add layers when they pay for themselves.
Easy to hard
Questions interviewers ask, easy to hard
- How do you structure an Express project?
- Where does business logic live? Where do DB queries live?
- How do you test a service without a real database?
- How do you manage configuration and secrets?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.