backend-mastery — Backend expert knowledge
What this covers
Framework-idiomatic patterns for request-response, middleware, error handling, and background processing. Ensures code follows how the framework WANTS the problem solved.
Core principle
Layer discipline. Business logic in services, not routes. Errors handled centrally, not per-handler. Resources always closed.
Key patterns (2026)
Express 5 — Native async (no wrappers)
// ❌ BEFORE: Express 4 async wrapper boilerplate
const asyncHandler = (fn) => (req, res, next) =>
Promise.resolve(fn(req, res, next)).catch(next);
app.get('/users', asyncHandler(async (req, res) => { ... }));
// ✅ AFTER: Express 5 native async
app.get('/users', async (req, res) => {
const users = await User.find();
res.json(users);
});
// Errors auto-forwarded to centralized error middleware
- Express 5 auto-catches promise rejections — no
express-async-errors needed
- Centralized error middleware:
(err, req, res, next) handles all errors
FastAPI — Dependency injection for layered security
# ✅ Layered security via Depends()
from fastapi import Depends, HTTPException
async def get_current_user(token: str = Depends(oauth2_scheme)):
user = await verify_token(token)
if not user:
raise HTTPException(status_code=401)
return user
@app.get("/protected")
async def protected_route(user = Depends(get_current_user)):
return {"user": user}
Django 5 — Security middleware
# settings.py — security headers via middleware
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
# ... HSTS, X-Frame-Options, CSP
]
Bypass signals to detect
- Business logic in route handlers (should be in services)
- DB queries in controllers (should be behind repository)
- Raw SQL string concat (should be parameterized)
catch (Exception e) { } swallowing errors
- Unclosed resources (connections, file handles, streams)
try/catch in every async handler (Express 5 handles this)
mark_safe() with user input (Django XSS vector)
Anti-patterns
- Error swallowing —
catch {} with no logging or re-throw
- Business logic in handlers — routes should delegate to services
- Missing input validation — validate at the boundary (Zod, Pydantic, Django forms)
- No rate limiting — auth endpoints MUST be rate-limited
- Missing security headers — Helmet (Express), SecurityMiddleware (Django)
How to verify
When triggered
explorer agent parallel dispatch when backend files detected
- Task mentions endpoint / route / handler / middleware / service / job / worker
paths glob auto-activates on server framework files
References
Source: KaosKyun/Ciel — distributed by TomeVault.
1---2name: backend-mastery3description: Expert patterns for backend server development across Ktor, Go net/http, Node/Express, Rails, Django, FastAPI, Spring — routing, middleware, authentication, background jobs, connection pooling, error handling. Auto-activates on server framework files. Use when this capability is needed.4---56# backend-mastery — Backend expert knowledge78## What this covers9Framework-idiomatic patterns for request-response, middleware, error handling, and background processing. Ensures code follows how the framework WANTS the problem solved.1011## Core principle12**Layer discipline.** Business logic in services, not routes. Errors handled centrally, not per-handler. Resources always closed.1314## Key patterns (2026)1516### Express 5 — Native async (no wrappers)1718```js19// ❌ BEFORE: Express 4 async wrapper boilerplate20const asyncHandler = (fn) => (req, res, next) =>21 Promise.resolve(fn(req, res, next)).catch(next);22app.get('/users', asyncHandler(async (req, res) => { ... }));2324// ✅ AFTER: Express 5 native async25app.get('/users', async (req, res) => {26 const users = await User.find();27 res.json(users);28});29// Errors auto-forwarded to centralized error middleware30```3132- Express 5 auto-catches promise rejections — no `express-async-errors` needed33- Centralized error middleware: `(err, req, res, next)` handles all errors3435### FastAPI — Dependency injection for layered security3637```python38# ✅ Layered security via Depends()39from fastapi import Depends, HTTPException4041async def get_current_user(token: str = Depends(oauth2_scheme)):42 user = await verify_token(token)43 if not user:44 raise HTTPException(status_code=401)45 return user4647@app.get("/protected")48async def protected_route(user = Depends(get_current_user)):49 return {"user": user}50```5152### Django 5 — Security middleware5354```python55# settings.py — security headers via middleware56MIDDLEWARE = [57 'django.middleware.security.SecurityMiddleware',58 # ... HSTS, X-Frame-Options, CSP59]60```6162## Bypass signals to detect6364- Business logic in route handlers (should be in services)65- DB queries in controllers (should be behind repository)66- Raw SQL string concat (should be parameterized)67- `catch (Exception e) { }` swallowing errors68- Unclosed resources (connections, file handles, streams)69- `try/catch` in every async handler (Express 5 handles this)70- `mark_safe()` with user input (Django XSS vector)7172## Anti-patterns7374- **Error swallowing** — `catch {}` with no logging or re-throw75- **Business logic in handlers** — routes should delegate to services76- **Missing input validation** — validate at the boundary (Zod, Pydantic, Django forms)77- **No rate limiting** — auth endpoints MUST be rate-limited78- **Missing security headers** — Helmet (Express), SecurityMiddleware (Django)7980## How to verify8182- [ ] Centralized error handling middleware present?83- [ ] Business logic in services, not route handlers?84- [ ] Input validated at the boundary (Zod/Pydantic/Django forms)?85- [ ] Resources closed (try-with-resources, `use`, `defer`, context managers)?86- [ ] Rate limiting on auth endpoints?87- [ ] Security headers configured (HSTS, CSP, X-Frame-Options)?88- [ ] Express 5: no async wrapper boilerplate (native async)?8990## When triggered9192- `explorer` agent parallel dispatch when backend files detected93- Task mentions endpoint / route / handler / middleware / service / job / worker94- `paths` glob auto-activates on server framework files9596## References9798- Express 5 async — https://expressjs.com/en/guide/migrating-5.html99- FastAPI security — https://fastapi.tiangolo.com/tutorial/security/100- Django security — https://docs.djangoproject.com/en/5.1/topics/security/101- OWASP Top 10 — https://owasp.org/www-project-top-ten/102103---104> Source: [KaosKyun/Ciel](https://github.com/KaosKyun/Ciel) — distributed by [TomeVault](https://tomevault.io).105<!-- tomevault:4.0:skill_md:2026-05-22 -->