Overview
Production mode in Express Zod API enables important optimizations and security enhancements. It’s activated by setting theNODE_ENV environment variable to production.
Enabling Production Mode
Set the environment variable before starting your server:What Changes in Production Mode
1. Express Performance Optimizations
Express automatically activates performance optimizations whenNODE_ENV=production:
- Template caching
- CSS caching
- Reduced overhead in error handling
- Optimized view rendering
2. Self-Diagnosis Disabled
The framework’s self-diagnosis for potential configuration problems is disabled to ensure faster startup:3. Error Message Security
The most important change is how error messages are handled. In production, server-side error details are generalized to prevent information disclosure.Error Message Behavior
Default Behavior
In production mode, thedefaultResultHandler, defaultEndpointsFactory, and LastResortHandler generalize server-side error messages:
Status Code Rules
Errors with 5XX status codes are generalized in production:Controlling Error Exposure
Use theexpose option in createHttpError() to control whether error messages are shown:
Complete Examples
Error Handling in Production
Custom Error with Expose Control
Logging in Production
Adjust logging levels for production:Structured Logging
Use structured logging in production for better monitoring:Environment-Specific Configuration
Security Best Practices
1. Never Expose Internal Errors
2. Use Appropriate Status Codes
3. Log Sensitive Errors Securely
Monitoring and Observability
Implement proper monitoring in production:Testing Production Behavior
Test production error handling:Checklist for Production
Production Deployment Checklist
Production Deployment Checklist
- Set
NODE_ENV=production - Configure appropriate logging level
- Enable compression
- Set up graceful shutdown
- Configure CORS properly
- Use HTTPS
- Set appropriate rate limits
- Enable error monitoring (Sentry, etc.)
- Review and sanitize error messages
- Test error handling in production mode
- Set up health check endpoints
- Configure proper timeout values
Related Topics
- Error Handling - Understanding error handling
- Configuration - Server configuration options
- Graceful Shutdown - Handling shutdowns properly
- Logging - Logger configuration