Router type.
Creating a router
Create a new router usingRouter::new():
404 Not Found to all requests until you add routes.
Adding routes
Use theroute method to add routes to your router. Each route consists of a path pattern and a MethodRouter that defines which HTTP methods are handled:
HTTP method routing
Axum provides convenience functions for each HTTP method:- GET
- POST
- PUT/PATCH
- DELETE
GET routes also handle HEAD requests automatically, but with the response body removed.Path patterns
Axum supports dynamic path segments using named parameters:Wildcard captures
Capture the rest of the path with wildcard parameters:Combining routes
Chaining routes
Chain multipleroute calls to add multiple routes:
Merging routers
Combine multiple routers withmerge:
Nesting routers
Nest a router under a path prefix:Fallback handlers
Handle requests that don’t match any route:Route services
For more control, you can route to any TowerService:
Placeholder handlers
Understanding placeholder handlers
Understanding placeholder handlers
You can pass types that implement
IntoResponse directly as handlers:Next steps
Handlers
Learn about handler functions and their signatures
State
Share state across handlers using
with_state