nodejs-backend-patterns
/nodejs-backend-patterns
Node.js backend architecture: use when building or refactoring Fastify/Express services that need fail-fast schema validation, layered controller/service/repository separation, pooled transactional data access, a single error envelope, or drain-based shutdown.
- Author
- j0hanz ↗
- Repository
- j0hanz-marketplace ↗
- Licence
- MIT
- Last repository change
- 27.10.2026
Assessment and security
- Files in this package
- 2
- Scripts
- 0
- HTML comments
- 0
- URLs in SKILL.md
- 0
No signals detected. Repository changed on 27.10.2026, without references to retired models or APIs.
MIT, open: use and modify while retaining author attribution.
What it does and when to use it
Node.js backend architecture: use when building or refactoring Fastify/Express services that need fail-fast schema validation, layered controller/service/repository separation, pooled transactional data access, a single error envelope, or drain-based shutdown.
Original author description
Node.js backend architecture: use when building or refactoring Fastify/Express services that need fail-fast schema validation, layered controller/service/repository separation, pooled transactional data access, a single error envelope, or drain-based shutdown.
How it works
- Stack Defaults
- Core Steps
- Disclosed Reference
SKILL.md preview
---
name: nodejs-backend-patterns
description: 'Node.js backend architecture: use when building or refactoring Fastify/Express services that need fail-fast schema validation, layered controller/service/repository separation, pooled transactional data access, a single error envelope, or drain-based shutdown.'
metadata:
category: reference
---
# Node.js Backend Patterns
Operational architecture guide for Node.js backends: `fail-fast` contracts, `layered` responsibilities, `pooled` data access, one `envelope`, graceful `drain`.
## Stack Defaults
- **Default**: Fastify + TypeScript + PostgreSQL (`pg`) + Zod + Pino.
- **Express**: Use when integrating into existing Express middleware pipelines.
- **MongoDB**: Use when document datastores are mandated by existing schema design.
## Core Steps
### 1. Define Boundary Contracts (`fail-fast`)
- Type every public route with explicit schemas for params, query, body, and responses.
- Validate at the HTTP layer; schema failures return 400 before reaching service logic.
- **Completion criterion**: Every public route defines strict schemas and returns 400 on schema failure.
### 2. Isolate Responsibilities (`layered`)
- **Controller**: Parse HTTP requests, delegate to service, return HTTP response codes.
- **Service**: Domain and business logic, authorization checks, transaction orchestration.
- **Repository**: Raw SQL/queries, data mapping, direct database pool interactions.
- Wire components using constructor-based dependency injection.
- **Completion criterion**: Every controller handles transport only (no SQL/DB imports); every service handles domain logic only (no HTTP request/response imports).
### 3. Manage Connections & Transactions (`pooled`)
- Configure database connection pools with explicit max connections, idle timeouts, and connection timeouts.
- Wrap multi-operation writes in atomic transactions (`BEGIN` / `COMMIT` / `ROLLBACK`).
- Release checked-out pool clients in `finally` blocks.
- **Completion criterion**: Every multi-statement write runs in a transaction that rolls back on error; every checked-out pool client is released on all execution paths.
### 4. Standardize Responses & Errors (`envelope`)
- Use a single top-level error handler mapping typed `AppError` subclasses to status codes.
- Return one JSON envelope for success (`{ status: "success", data }`) and failure (`{ status: "error", code, message }`); sanitize messages in production.
- **Completion criterion**: Every request-scoped error passes through the global error handler and returns the failure envelope with a sanitized message.
### 5. Wire Process Lifecycle (`drain`)
- Bind `SIGTERM` and `SIGINT` to a coordinated shutdown handler.
- Stop accepting new HTTP requests, drain in-flight requests within a grace timeout (e.g., 10s), then close DB pools and cache connections.
- Keep the force-exit timer unref'd so it never holds the event loop open.
- **Completion criterion**: Application exits with code 0 on termination signal after in-flight connections drain and pools close.
## Disclosed Reference
Implementation blueprints — schema definitions, DI container, pool configuration, transaction helper, error handler, shutdown handler — in [`resources/implementation-playbook.md`](resources/implementation-playbook.md). Load when implementing any step.
Files in this package (2)
- SKILL.md 3 KB
- resources/implementation-playbook.md 8 KB
Versions
| Repository | Score | Licence |
|---|---|---|
| This page · j0hanz/j0hanz-marketplace | 90 | MIT |
| rootcastleco/rei-skills | 100 | MIT |
| wshobson/agents | 100 | MIT |
| HermeticOrmus/LibreUIUX-Claude-Code | 91 | MIT |
| LeoYeAI/openclaw-master-skills | 91 | MIT |
Database snapshot: 13.11.2026 · package version 75553979cc63 (SHA-256 digest of the entire package)
Downloads in the last 30 days: 0 · ranking
Report a problem with this skill: harmful content, incorrect descriptions or an author’s removal request.
Similar skills
- 100nodejs-backend
Node.js backend patterns: framework selection, layered architecture, TypeScript, validation, error handling, security, production deployment. Use when building REST APIs, Express/Fastify servers, microservices, or server-side TypeScript. - 100nodejs-backend
Guides Node.js backend services built on Fastify, NestJS, Hono or Express. Use when writing, reviewing or hardening a Node.js HTTP service or moving one off Express, or a service leaks stack traces, drops in-flight requests on deploy, logs the wrong user, or blocks the event loop. - 100nodejs-backend-expert
Expert Node.js backends (Express/Fastify/Hono): routing, middleware, validation, auth, async, and production hardening. Trigger keywords: Node.js, Express, Fastify, Hono, REST, middleware, JWT, session, zod, async, streams, unhandled rejection, graceful shutdown, backend,… - 90nodejs-architecture
Use this skill when designing Node.js backend architecture — Express, Fastify, Hono. Project structure, middleware pipeline, routing, error handling, validation. This skill enforces: separation of concerns, correct middleware ordering, global error handling, Zod/Joi validation… - 90nodejs-fastify
Use this skill when building Fastify applications — schema validation, plugin system, hooks, serialization. This skill enforces: JSON Schema validation, plugin encapsulation, schema serializers, Fastify lifecycle hooks. Do NOT use for: Express.js apps, database schema, frontend,… - 100backend-patterns
Applies production backend patterns: middleware, error handling, auth, database integration, and API design. Use when working with backend service files or when the user mentions Express, Fastify, NestJS, backend patterns, or service architecture.