--- title: Backend tags: - backend - express - node --- # Backend ## Overview Node.js + Express REST API serving the [[Frontend]] via JSON. Uses Sequelize ORM with a MySQL database named **Showzy**. ## Entry Point — `server.js` 1. Creates Express app with `body-parser` (JSON + URL-encoded) 2. CORS: allows all origins (`*`) 3. All API routes mounted under `/api/v1` via `routes/routes.js` 4. Error handler middleware at the end (see [[Error Handling]]) 5. Debug crash route at `/killer` (outside `/api/v1`) 6. Sequelize sync: `sequelize.sync({ alter: doUpdate })` where `doUpdate = false` 7. Calls `initializeData()` after sync — seeds 27 TMDB genres if needed 8. Listens on port from `settings.json` (default `4000`) 9. Process-level handlers for `unhandledRejection` and `uncaughtException` ## Project Structure ``` backend/ ├── server.js Entry point ├── package.json Dependencies ├── nodemon.json Dev config (watches .js, .json) ├── settings.json Runtime config (see [[Env]]) ├── config/ │ ├── database.js Sequelize instance → MySQL "Showzy" │ ├── relations.js All model associations → [[Database#Relationships]] │ ├── dataInitializer.js Genre seeding (27 TMDB genres) │ └── sequelizeHooks.js UserCollectionEntry hooks (order management) ├── controllers/ Route handlers (14 controllers) ├── services/ Business logic (16 services) → [[Services]] ├── models/ Sequelize models (17 models) → [[Models]] ├── routes/ Express routers (14 route files) → [[API Reference]] ├── errors/ Custom error classes → [[Error Handling]] ├── middleware/ │ └── errorHandler.js Central error catching ├── mappers/ │ └── entryMapper.js Entry response mapping └── utils/ ├── asyncWrapper.js Async route error wrapper └── moment.js Bundled Moment.js 2.29.0 ``` ## Middleware Pipeline ``` Request → body-parser → CORS → /api/v1/routes → Controller → Service → Response ↓ (on error) errorHandler middleware ``` ## Configuration Backend reads `settings.json` at startup. See [[Env]] for all fields. Key config files: - `config/database.js` — Sequelize connection to MySQL `Showzy` on `localhost` (root/password) - `config/relations.js` — All model associations (see [[Database#Relationships]]) - `config/dataInitializer.js` — Seeds genre table with 27 TMDB genres - `config/sequelizeHooks.js` — Auto-manages `order` field on [[Collections|UserCollectionEntry]] ## Key Patterns ### Async Wrapper All route handlers are wrapped with `asyncWrapper()` which catches rejected promises and forwards to `next(err)` → [[Error Handling]]. ### Singleton Services All services use the singleton pattern — `if (!Class.instance) Class.instance = this`. ### Lazy Refresh [[Services|EntryManager]] and [[Services|PeopleManager]] implement lazy refresh: data is only re-fetched from TMDB if `last_fetch` is null or older than 24 hours. ### Media Pipeline See [[Media System]] for the complete download → optimize → serve flow. ## See Also - [[API Reference]] — All endpoints - [[Database]] — Schema and models - [[Services]] — Business logic - [[Error Handling]] — Error classes and middleware - [[TMDB Integration]] — External API - [[Media System]] — Image handling