Backend Project Structure and Layered Architecture
🎯 Introduction
By the end of this article, you will be able to locate files by feature and decide when splitting directories is worthwhile.
🗂 Make directories reflect actual maintenance tasks
A user query feature can use this structure:
backend-demo/
src/
app.mjs
server.mjs
controllers/user-controller.mjs
services/user-service.mjs
repositories/user-repository.mjs
config.mjs
test/
user.test.mjs
.env
.gitignore
The division should answer “Which file should I change for this requirement?”:
| Change | First file to inspect |
|---|---|
| Change an endpoint URL or add shared middleware | app.mjs |
| Change the listening port or handle startup and shutdown | server.mjs |
| Change request parameters or HTTP statuses | controllers/user-controller.mjs |
| Add business rules such as account state checks | services/user-service.mjs |
| Change database query fields | repositories/user-repository.mjs |
| Change how ports and database addresses are read | config.mjs |
Controller, Service, and Repository handle requests, business rules, and database access, respectively. The next article demonstrates their calls with a real query; here we establish file locations.
🔌 Separate app definition from listening
app.mjs creates the app and registers routes; server.mjs imports it and listens on a port. Tests can then use app directly without occupying the development port merely by importing a file.
Connect each new feature's routes to app and use the shared startup file. Do not add listen to every feature file.
✂️ When should files be split?
An endpoint with only a few lines can start in one file. Split when:
- Several endpoints share a user rule: move it into a Service so changes happen in one place.
- SQL obscures parameter and response logic: move queries into a Repository.
- One file mixes user, file, and messaging endpoints: split by feature to reduce interference.
Each extracted function should have a clear job. If a function merely forwards arguments unchanged, has no independent rule, and needs no reuse, do not add files just to fill three layers.
🔒 Where should configuration live?
A local .env stores settings such as ports and database addresses. Exclude files containing real credentials with .gitignore. Commit an .env.example with example values so collaborators know which variables are required.
Keep keys, uploaded files, and logs out of source directories. The configuration and logging article in this chapter covers implementation details.
❓ Questions and Answers
Q1: Should these directories be copied when switching to Egg?
Follow Egg's directory conventions; the responsibility boundaries still apply. See the Egg course for automatically loaded Controllers and Services.
Q2: Does a well-structured endpoint need many files?
File count is not the goal. Being able to find and change a feature clearly is what matters.
Q3: Should every feature create its own database connections?
Usually, share a connection pool, control the total connection count, and close it centrally on shutdown.
🧪 Practice
For “update a nickname and return the new profile,” identify the files for parameter checks, business rules, and database updates. You do not need to create empty files first.
// Record files and their responsibilities here
