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
end

Route 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

  1. Method Check: Verify HTTP method matches

  2. Path Traversal: Walk the route tree

  3. Parameter Extraction: Extract parameters from path

  4. Constraint Validation: Validate parameter constraints

  5. 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

3. Use Descriptive Route Names

4. Apply Constraints

5. Use Namespaces

Next Steps

Now that you understand routing:

  1. Endpoints - Use routes in your endpoints

  2. Middleware - Apply middleware to routes

  3. Testing - Test your routes

  4. API Design - Design RESTful APIs

  5. 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?