CORS Configuration
Configure Cross-Origin Resource Sharing (CORS) in MoroJS to enable secure cross-domain requests while maintaining proper security boundaries.
Basic CORS Setup
MoroJS provides built-in CORS support with intelligent defaults and comprehensive configuration options for production environments.
Simple CORS Configuration
typescript
Default Behavior
When CORS is enabled, MoroJS automatically:
- • Handles preflight OPTIONS requests
- • Sets appropriate CORS headers
- • Validates origin against allowed list
- • Manages credential handling
- • Optimizes for performance
Advanced Configuration
Comprehensive CORS Configuration
typescript
Environment-Specific CORS
Environment-Based Configuration
typescript
Dynamic CORS with Database
typescript
Route-Specific CORS
Per-Route CORS Configuration
typescript
Troubleshooting CORS
Common Issues
- • Origin not in allowed list
- • Missing credentials configuration
- • Incorrect preflight handling
- • Wrong HTTP method allowed
- • Missing required headers
- • Browser cache issues
Best Practices
- • Use specific origins in production
- • Enable credentials only when needed
- • Set appropriate cache duration
- • Minimize allowed headers
- • Log CORS rejections
- • Test with different browsers
CORS Debugging Middleware
typescript
CORS Testing Utility
typescript