
Nodejs Backend Patterns
Stand up or harden Express/Fastify APIs with consistent middleware, auth, validation, and database patterns before you ship solo SaaS or agent backends.
Install
npx skills add https://github.com/wshobson/agents --skill nodejs-backend-patternsWhat is this skill?
- Express and Fastify middleware, routing, and scalable service layout
- Authentication, authorization, and custom error-class handling
- SQL and NoSQL integration with input validation (Zod or Joi)
- WebSockets and background job processing patterns
- Eight documented best practices: TypeScript, env secrets, structured logging, rate limiting, HTTPS
Adoption & trust: 34.6k installs on skills.sh; 36.5k GitHub stars; 3/3 security scanners passed (skills.sh audits).
Recommended Skills
Journey fit
Backend architecture and API design land in the build phase when you are implementing servers, not when you are only validating an idea. REST, GraphQL, microservices, WebSockets, and job queues are core backend subphase work distinct from frontend or agent packaging.
Common Questions / FAQ
Is Nodejs Backend Patterns safe to install?
skills.sh reports 3 of 3 security scanners passed. Review the Security Audits panel on this page before installing in production.
SKILL.md
READMESKILL.md - Nodejs Backend Patterns
# Node.js Backend Patterns Comprehensive guidance for building scalable, maintainable, and production-ready Node.js backend applications with modern frameworks, architectural patterns, and best practices. ## When to Use This Skill - Building REST APIs or GraphQL servers - Creating microservices with Node.js - Implementing authentication and authorization - Designing scalable backend architectures - Setting up middleware and error handling - Integrating databases (SQL and NoSQL) - Building real-time applications with WebSockets - Implementing background job processing ## Detailed patterns and worked examples Detailed pattern documentation lives in `references/details.md`. Read that file when the navigation tier above is insufficient. ## Best Practices 1. **Use TypeScript**: Type safety prevents runtime errors 2. **Implement proper error handling**: Use custom error classes 3. **Validate input**: Use libraries like Zod or Joi 4. **Use environment variables**: Never hardcode secrets 5. **Implement logging**: Use structured logging (Pino, Winston) 6. **Add rate limiting**: Prevent abuse 7. **Use HTTPS**: Always in production 8. **Implement CORS properly**: Don't use `*` in production 9. **Use dependency injection**: Easier testing and maintenance 10. **Write tests**: Unit, integration, and E2E tests 11. **Handle graceful shutdown**: Clean up resources 12. **Use connection pooling**: For databases 13. **Implement health checks**: For monitoring 14. **Use compression**: Reduce response size 15. **Monitor performance**: Use APM tools ## Testing Patterns See `javascript-testing-patterns` skill for comprehensive testing guidance. # Node.js Advanced Patterns Advanced patterns for dependency injection, database integration, authentication, caching, and API response formatting. ## Dependency Injection ### DI Container ```typescript // di-container.ts import { Pool } from "pg"; import { UserRepository } from "./repositories/user.repository"; import { UserService } from "./services/user.service"; import { UserController } from "./controllers/user.controller"; import { AuthService } from "./services/auth.service"; class Container { private instances = new Map<string, any>(); register<T>(key: string, factory: () => T): void { this.instances.set(key, factory); } resolve<T>(key: string): T { const factory = this.instances.get(key); if (!factory) { throw new Error(`No factory registered for ${key}`); } return factory(); } singleton<T>(key: string, factory: () => T): void { let instance: T; this.instances.set(key, () => { if (!instance) { instance = factory(); } return instance; }); } } export const container = new Container(); // Register dependencies container.singleton( "db", () => new Pool({ host: process.env.DB_HOST, port: parseInt(process.env.DB_PORT || "5432"), database: process.env.DB_NAME, user: process.env.DB_USER, password: process.env.DB_PASSWORD, max: 20, idleTimeoutMillis: 30000, connectionTimeoutMillis: 2000, }), ); container.singleton( "userRepository", () => new UserRepository(container.resolve("db")), ); container.singleton( "userService", () => new UserService(container.resolve("userRepository")), ); container.register( "userController", () => new UserController(container.resolve("userService")), ); container.singleton( "authService", () => new AuthService(container.resolve("userRepository")), ); ``` ## Database Patterns ### PostgreSQL with Connection Pool ```typescript // config/database.ts import { Pool, PoolConfig }