★ Featured ✓ FRESH Verified against Node 22.0.0, Express 5.1.0, Typescript 5.6.0 - Mar 12, 2026 by FindUtils

Node.js + Express Best Practices

Production-ready Node.js with Express, error handling, middleware, and security.

Claude CodeCursorGitHub CopilotWindsurfClineCodex / OpenAIGemini CLI
Updated 2026-04-05
CLAUDE.md
# Node.js + Express Best Practices

You are an expert in Node.js, Express, TypeScript, and backend development.

Architecture:
- Use TypeScript strict mode for all code
- Structure by feature/domain, not by type (routes/, controllers/, models/)
- Use dependency injection for testability
- Implement proper error handling middleware as the last middleware
- Use async/await; never mix callbacks and promises

Middleware:
- Order matters: security headers > body parsing > auth > routes > error handler
- Use helmet() for security headers
- Use cors() with explicit origin allowlist
- Rate limiting on all public endpoints
- Request validation middleware with Zod or Joi

Error Handling:
- Create custom error classes extending Error
- Use a centralized error handler middleware
- Never expose stack traces in production
- Log errors with structured logging (pino, winston)
- Return consistent error response format

Security:
- Validate and sanitize all inputs
- Use parameterized queries; never string concatenation
- Implement CSRF protection for cookie-based auth
- Set secure cookie flags: httpOnly, secure, sameSite
- Rate limit authentication endpoints aggressively

Performance:
- Use connection pooling for databases
- Implement response caching with proper Cache-Control
- Use streaming for large responses
- Implement graceful shutdown handling
- Use cluster mode or PM2 for multi-core utilization

Add to your project root CLAUDE.md file, or append to an existing one.

Quality Score

Rank A Average: 8.9/10
Practicality
9.0
Clarity
8.5
Actionability
9.0
Freshness
9.2
Impact
8.8

Tags

nodejsexpresstypescriptapimiddlewaresecurity