Current Size:
Chakra BreakPoints
base0px
sm480px
md768px
lg992px
xl1280px
2xl1536px
Current Height:Width
widthpx
heightpx

Subjects

Organizing Express API

Date Created: 2021/08/26

Last Update: 2023/07/01

#api #express #node #structure

Directory Structure Example

Example One

src/
├── app.js --- app entry point
├── /api --- controller layer: api routes
├── /config --- config settings, env variables
├── /services --- service layer: business logic
├── /models --- data access layer: database models
├── /scripts --- miscellaneous NPM scripts
├── /subscribers --- async event handlers
└── /test --- test suites

Example Two

src/
|- controllers/ -> logic before saving to the db, check permission, etc..
|- db/
|-- models/ -> the models
|-- config/ -> config about the db, connection to the db etc
|-- migrations/ -> all the migrations file for the db
|- helpers/ -> helpers function like sum, total, pluralize, etc
|- routes/ -> all the rest api route, where they take a controller as callback
|- services/ -> stripe, aws s3 etc
|- test -> all your test
|-package.json -> all your dependencies
|-index.js -> where everything start, your server instance etc

MVC

MVC Example - intermediate

src/
|- api/ --- everything related to routes
|- controllers/ --- valiadate data
|- models/ --- models and schemas
|- helpers/ --- generic helpers functions that maybe used in many places
|- services/ --- business logic, data transforms and calls to the database
|- routes/ --- routes
|- middlewares/ --- routes
|- validation/ ---
|-- config/ --- config files
|- test
|-package.json
|-index.js

Example - Startup/Enterprise

  • use typeScript
test/
src/
|- api/ --- everything related to routes
|- controllers/ --- valiadate data
|- models/ --- models and schemas
|- helpers/ --- generic helpers functions that maybe used in many places
|- services/ --- business logic, data transforms and calls to the database
|- routes/ --- routes
|- middlewares/ --- routes
|- validation/ ---
|-- config/ --- config files
|-package.json
|-index.js

Layers

Data Layer

| Main Layer | Type | What logic goes here? | | -------------------- | ---------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | HTTP logic layer | Routes + Controllers | Routes - handle the HTTP requests that hits the API and route them to appropriate controller(s) Contollers - take request object, pull out data from request, validate, then send to service(s) | | Business logic layer | Services + Data Access | Contains the business logic, derived from business and technical requirements, as well as how we access our data stores** | | | | |

ORM

Reference

Articles

Videos

More Notes

All Notes
HomeProjects

Links

Home Articles Notes Projects About Style Guide Site Credits

Contact

 [email protected]

Location

🌎 Earth