express
Express.js server best practices including middleware, error handling, and security.
When & Why to Use This Skill
This Claude skill provides a comprehensive framework for implementing Express.js server best practices, focusing on robust middleware integration, centralized error handling, and production-grade security. It streamlines the development of scalable Node.js applications by enforcing industry-standard project structures and performance optimization techniques to ensure code maintainability and system reliability.
Use Cases
- Architecting a scalable Express.js project structure using dedicated directories for routes, controllers, and business logic to improve code organization.
- Implementing a centralized error handling middleware to manage asynchronous errors consistently and prevent sensitive stack traces from leaking in production.
- Hardening server security by configuring essential headers with Helmet, managing CORS, and implementing strict input validation using Joi or Zod.
- Optimizing API performance through the integration of Redis caching, Gzip compression, and efficient database connection pooling.
| name | express |
|---|---|
| description | Express.js server best practices including middleware, error handling, and security. |
| globs | ["**/server.js", "**/app.js", "**/routes/**/*.js", "**/middleware/**/*.js"] |
| priority | 85 |
| tags | ["framework"] |
Express.js Best Practices
Project Structure
- src/routes/ for route handlers
- src/middleware/ for middleware
- src/controllers/ for business logic
- src/models/ for data models
- src/utils/ for utilities
Middleware
- Use helmet for security headers
- Use cors for CORS handling
- Use morgan for logging
- Use compression for gzip
- Use express.json() for body parsing
- Order middleware correctly
Error Handling
- Use async error wrapper or express-async-errors
- Centralized error handler middleware
- Send appropriate status codes
- Log errors with context
- Never expose stack traces in production
Security
- Validate all inputs with Joi/Zod
- Sanitize user data
- Use rate limiting
- Implement CSRF protection
- Use secure session config
Performance
- Use caching (Redis)
- Implement pagination
- Use streaming for large responses
- Enable gzip compression
- Use connection pooling for databases