| [Previous][Up] |
The HTTP router operates on a simple principle: you register routes that map URL patterns to handler code, and when an HTTP request arrives, the router finds the best matching route and executes its handler.
The request processing workflow consists of five main steps:
1. Route Registration: Use RegisterRoute to associate URL patterns with handlers. Each route specifies a URL pattern (like /users/:id), an HTTP method (GET, POST, etc.), and a handler (callback, event, interface, or object class).
2. Request Processing: When RouteRequest is called with an incoming HTTP request, the router searches through registered routes to find matches based on the request's path and HTTP method.
3. Pattern Matching: The router supports parameterized routes using colon notation (like :id). Parameters are extracted from the URL and made available to handlers through string lists.
4. Handler Execution: Once a matching route is found, its associated handler is executed with the HTTP request and response objects, along with any extracted parameters.
5. Interceptor Processing: Before and after route handling, registered interceptors can process the request and response for cross-cutting concerns like logging, authentication, and CORS.
The router prioritizes routes based on specificity, with exact matches taking precedence over parameterized routes, and parameterized routes taking precedence over default routes.