Intelligent Routing
MoroJS features an intelligent routing system that automatically optimizes middleware ordering, provides type-safe validation, and enhances performance through smart route compilation.
What is Intelligent Routing?
Traditional frameworks require you to manually order middleware and handle route compilation. MoroJS's intelligent routing system automatically:
- Optimizes middleware order based on dependencies and performance characteristics
- Compiles routes at startup for maximum runtime performance
- Provides type safety throughout the entire request pipeline
- Enables intelligent caching and rate limiting per route
Basic Route Definition
Simple Routes
typescript
Automatic Type Inference
MoroJS automatically infers parameter types from your route patterns. The params.id
in the example above is automatically typed as string
.
Advanced Route Configuration
Route with Full Configuration
typescript
Intelligent Middleware Ordering
MoroJS automatically orders middleware based on dependencies. Authentication runs before rate limiting, validation happens early, and caching is optimally placed for maximum efficiency.
Route Patterns
Supported Route Patterns
typescript
Intelligent Middleware System
Middleware Types and Ordering
typescript
Automatic Ordering
- 1Request parsing
- 2Authentication
- 3Authorization
- 4Validation
- 5Rate limiting
- 6Caching
- 7Handler execution
Performance Benefits
- No manual ordering needed
- Optimal execution path
- Early exit on failures
- Compile-time optimization
- Type-safe context passing
Route Compilation & Performance
Route Compilation Process
At application startup, MoroJS analyzes all your routes and creates an optimized execution plan:
- Builds optimal middleware chains
- Pre-compiles route matchers
- Generates type-safe handlers
- Optimizes for common patterns
Route Compilation Example
typescript
Best Practices
Do
- • Use descriptive route patterns
- • Define validation schemas
- • Group related routes together
- • Leverage automatic middleware ordering
- • Use type-safe parameter extraction
- • Define response schemas for documentation
Don't
- • Manually order middleware unnecessarily
- • Skip input validation
- • Use overly complex route patterns
- • Ignore TypeScript warnings
- • Mix business logic in middleware
- • Forget to handle errors properly