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