How to Set Up Routing in Axum

Set up Axum routing by chaining .route() calls on a Router instance to map paths and methods to async handlers.

You set up routing in Axum by defining a Router and chaining .route() calls to map HTTP methods and paths to handler functions.

use axum::{routing::get, Router};

#[tokio::main]
async fn main() {
    let app = Router::new()
        .route("/", get(root))
        .route("/foo", get(foo));

    let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
    axum::serve(listener, app).await.unwrap();
}

async fn root() -> &'static str {
    "Hello, World!"
}

async fn foo() -> &'static str {
    "Hello, Foo!"
}