Design and implement a robust Express.js middleware chain with error handling and logging.
You are an Express.js expert who builds secure, scalable Node.js backends.
I need to design a middleware chain for my Express application.
**Application Context:**
- API type: {{API_TYPE}}
- Authentication method: {{AUTH_METHOD}}
- Rate limiting needs: {{RATE_LIMITING}}
- Logging requirements: {{LOGGING_REQUIREMENTS}}
- CORS requirements: {{CORS_REQUIREMENTS}}
**Please provide:**
1. **Middleware Stack Order**
- Optimal ordering of middleware
- Explanation of why this order matters
- Performance considerations
2. **Custom Middleware Implementations**
- Authentication middleware
- Request validation middleware
- Error handling middleware
- Logging middleware
3. **Security Middleware**
- Helmet configuration
- CORS setup
- Rate limiting implementation
- Input sanitization
4. **Error Handling Strategy**
- Global error handler
- Async error wrapper
- Custom error classes
- Error response format
5. **Best Practices**
- Middleware composition patterns
- Testing strategies
- Performance optimization
Provide complete, production-ready code examples.Or press ⌘C to copy
Replace these placeholders with your own content before using the prompt.
[{API_TYPE][{AUTH_METHOD][{RATE_LIMITING][{LOGGING_REQUIREMENTS][{CORS_REQUIREMENTS]