Routing
Routing in Azu is built on a high-performance routing tree that provides type-safe, efficient URL handling with support for parameters, constraints, and nested routes.
What is Routing?
Routing maps HTTP requests to specific endpoints based on:
URL Pattern: The path pattern to match
HTTP Method: The HTTP method (GET, POST, PUT, DELETE, etc.)
Endpoint: The endpoint that handles the request
Parameters: URL parameters extracted from the path
Basic Routing
Simple Routes
module MyApp
include Azu
router do
# Root route
root :web, HomeEndpoint
# Simple routes
get "/about", AboutEndpoint
get "/contact", ContactEndpoint
post "/contact", ContactFormEndpoint
end
endRoute Groups
Organize related routes:
HTTP Methods
Azu supports all standard HTTP methods:
Route Parameters
Path Parameters
Extract parameters from the URL path:
Accessing Parameters
Access parameters in your endpoints:
Parameter Types
Parameters are automatically converted to the appropriate type:
Query Parameters
Handle query string parameters:
Route Constraints
Add constraints to route parameters:
Nested Routes
Organize complex route hierarchies:
Route Namespaces
Organize routes by namespace:
Route Scoping
Apply common settings to groups of routes:
Route Helpers
Generate URLs for your routes:
Route Testing
Test your routes:
Route Debugging
Debug your routes:
Route Performance
Azu's routing is optimized for performance:
Route Tree
Routes are organized in a tree structure for efficient matching:
Matching Algorithm
Method Check: Verify HTTP method matches
Path Traversal: Walk the route tree
Parameter Extraction: Extract parameters from path
Constraint Validation: Validate parameter constraints
Endpoint Resolution: Return the matching endpoint
Route Caching
Cache routes for better performance:
Route Middleware
Apply middleware to specific routes:
Route Validation
Validate routes at compile time:
Route Documentation
Document your routes:
Best Practices
1. Use RESTful Conventions
2. Group Related Routes
3. Use Descriptive Route Names
4. Apply Constraints
5. Use Namespaces
Next Steps
Now that you understand routing:
Endpoints - Use routes in your endpoints
Middleware - Apply middleware to routes
Testing - Test your routes
API Design - Design RESTful APIs
Performance - Optimize route performance
Routing in Azu provides a powerful, type-safe way to organize your application's URL structure. With support for parameters, constraints, and nested routes, it makes building complex web applications straightforward and maintainable.
Last updated
Was this helpful?
