Node.js Backend
Verify before implementing: For framework-specific APIs (Express 5, Fastify 5, Node.js 22+ built-ins), look up current docs via Context7 (query-docs) before writing code. Training data may lag current releases.
Working rules
- Validate request and third-party data before use; keep response serialization and error envelopes explicit.
- Preserve caller-visible contracts and authorization when adding resilience or fallbacks.
- Bound concurrency, set timeouts, and avoid blocking production request paths.
- Verify actual resource identity before parsing or caching a reused client's result.
- Exercise operational telemetry and failure paths, not successful return codes alone.
Architecture
src/
├── routes/ # HTTP: parse request, call service, format response
├── middleware/ # Auth, validation, rate limiting, logging
├── services/ # Business logic (no HTTP types)
├── repositories/ # Data access only (queries, ORM)
├── config/ # Env, DB pool, constants
└── types/ # Shared TypeScript interfaces
- Routes never contain business logic
- Services never import Request/Response
- Repositories never throw HTTP errors
- Dependencies point inward only (Clean Architecture rule): routes -> services -> repositories. Never the reverse.
- For scripts/prototypes: single file is fine -- ask "will this grow?"
TypeScript Rules
- Use
import type { } for type-only imports -- eliminates runtime overhead
- Prefer
interface for object shapes (2-5x faster type resolution than intersections)
- Prefer
unknown over any -- forces explicit narrowing
- Use
z.infer<typeof Schema> as single source of truth -- never duplicate types and schemas
- Minimize
as assertions -- use type guards instead
- Add explicit return types to exported functions (faster declaration emit)
- Untyped package?
declare module 'pkg' { const v: unknown; export default v; } in types/ambient.d.ts
Discipline
- Simplicity first -- every change as simple as possible, impact minimal code
- Only touch what's necessary -- avoid introducing unrelated changes
- No hacky workarounds -- if a fix feels wrong, step back and implement the clean solution
- Before adding a new abstraction, verify it appears in 3+ places. If not, inline it.
- If a fix requires bypassing TypeScript (
as any, non-null assertions on untrusted data, // @ts-ignore), treat it as a design smell and find the typed solution
Verify
tsc --noEmit passes with zero errors
npm test passes with zero failures
- No TypeScript bypasses (
as any, @ts-ignore) in new code
References
- TypeScript config -- tsconfig, ESM, branded types, compiler performance
- Security -- JWT, password hashing, rate limiting, OWASP
- API design patterns -- pagination, filtering, sorting, deprecation, idempotency-key claim and retention
- Database & production -- connection pooling, transactions, Docker, logging
Task-specific references
Read the relevant reference before implementing or reviewing the matching behavior:
Existing specialized references, when the corresponding topic applies:
1---2name: ia-nodejs-backend3description: Node.js backend patterns: layered architecture, TypeScript, validation, error handling, security, observability, logging, metrics, deployment. Use when building REST APIs, REST endpoints, middleware, Express/Fastify/Hono/NestJS/Koa servers, tRPC procedures, Bun servers, or server-side TypeScript.4---56# Node.js Backend78**Verify before implementing**: For framework-specific APIs (Express 5, Fastify 5, Node.js 22+ built-ins), look up current docs via Context7 (`query-docs`) before writing code. Training data may lag current releases.910## Working rules1112- Validate request and third-party data before use; keep response serialization and error envelopes explicit.13- Preserve caller-visible contracts and authorization when adding resilience or fallbacks.14- Bound concurrency, set timeouts, and avoid blocking production request paths.15- Verify actual resource identity before parsing or caching a reused client's result.16- Exercise operational telemetry and failure paths, not successful return codes alone.1718## Architecture1920```21src/22├── routes/ # HTTP: parse request, call service, format response23├── middleware/ # Auth, validation, rate limiting, logging24├── services/ # Business logic (no HTTP types)25├── repositories/ # Data access only (queries, ORM)26├── config/ # Env, DB pool, constants27└── types/ # Shared TypeScript interfaces28```2930- Routes never contain business logic31- Services never import Request/Response32- Repositories never throw HTTP errors33- Dependencies point inward only (Clean Architecture rule): routes -> services -> repositories. Never the reverse.34- For scripts/prototypes: single file is fine -- ask "will this grow?"353637## TypeScript Rules3839- Use `import type { }` for type-only imports -- eliminates runtime overhead40- Prefer `interface` for object shapes (2-5x faster type resolution than intersections)41- Prefer `unknown` over `any` -- forces explicit narrowing42- Use `z.infer<typeof Schema>` as single source of truth -- never duplicate types and schemas43- Minimize `as` assertions -- use type guards instead44- Add explicit return types to exported functions (faster declaration emit)45- Untyped package? `declare module 'pkg' { const v: unknown; export default v; }` in `types/ambient.d.ts`464748## Discipline4950- Simplicity first -- every change as simple as possible, impact minimal code51- Only touch what's necessary -- avoid introducing unrelated changes52- No hacky workarounds -- if a fix feels wrong, step back and implement the clean solution53- Before adding a new abstraction, verify it appears in 3+ places. If not, inline it.54- If a fix requires bypassing TypeScript (`as any`, non-null assertions on untrusted data, `// @ts-ignore`), treat it as a design smell and find the typed solution555657## Verify5859- `tsc --noEmit` passes with zero errors60- `npm test` passes with zero failures61- No TypeScript bypasses (`as any`, `@ts-ignore`) in new code626364## References6566- [TypeScript config](./references/typescript-config.md) -- tsconfig, ESM, branded types, compiler performance67- [Security](./references/security.md) -- JWT, password hashing, rate limiting, OWASP68- [API design patterns](./references/api-design.md) -- pagination, filtering, sorting, deprecation, idempotency-key claim and retention69- [Database & production](./references/database-production.md) -- connection pooling, transactions, Docker, logging7071## Task-specific references7273Read the relevant reference before implementing or reviewing the matching behavior:7475- For framework choice, input validation, API contracts, or errors: [api-boundaries.md](./references/api-boundaries.md).76- For concurrency, networking, startup, caches, lifecycle cleanup, or telemetry: [async-and-production.md](./references/async-and-production.md).7778Existing specialized references, when the corresponding topic applies: