expression

Axum: your first GET route

A router selects a handler by URL path and method. The handler is an asynchronous function whose return value becomes an HTTP response. Calling the router with a constructed request lets you study routing without starting a server. In-memory standalone Cargo example. No socket is opened. Expected stdout (not executed): 200 ready Common mistake: Calling the handler directly and assuming it verifies the routing configuration.

Formal Syntax & Structure

rust syntaxrust
Router::new().route(path, get(handler))

Working Example

Example Snippetrust
use axum::{Router, body::{Body, to_bytes}, http::{Request, StatusCode}};use tower::ServiceExt;
#[tokio::main(flavor = "current_thread")]async fn main() {    use axum::routing::get;    async fn health() -> &'static str { "ready" }    let app = Router::new().route("/health", get(health));    let response = app.oneshot(Request::builder().uri("/health").body(Body::empty()).unwrap()).await.unwrap();    println!("{}", response.status().as_u16());    let bytes = to_bytes(response.into_body(), 32).await.unwrap();    println!("{}", std::str::from_utf8(&bytes).unwrap());}
Rust Reference