Guide - Guides

How do I create a minimal web server with axum?

#axum#web#getting-started#tokiopublished 0 replies0 views
ReplyWatch
Share
Hacker NewsRedditLinkedInE-mail

Short answer

Create a binary crate, add axum and tokio to Cargo.toml, build a Router with one route, bind a TcpListener and pass both to axum::serve. Run it with cargo run and open the address in a browser or curl. The whole program is a few lines long.

Checked with rustc 1.93.0, axum 0.8.9, tokio 1.54.1, Windows 11; cargo run and curl.exe against the running program

Guide written by the Ruststudio team. Replies are open: corrections are welcome.

#1

What you will build#

A program that answers GET / with the text hello from axum on 127.0.0.1:3001.

Before you start#

  • Rust installed (rustc --version prints a version).

Steps#

1. Create the crate and add the dependencies#

text
cargo new hello-web
cd hello-web
Cargo.tomltoml
[dependencies]
axum = "0.8.9"
tokio = { version = "1.54.1", features = ["full"] }

2. Write the server#

#[tokio::main] starts the async runtime. Router maps a path and method to a handler; the closure || async { ... } is a handler that returns a string.

src/main.rsrust
use axum::{routing::get, Router};

#[tokio::main]
async fn main() {
    let app = Router::new().route("/", get(|| async { "hello from axum" }));
    let listener = tokio::net::TcpListener::bind("127.0.0.1:3001").await.unwrap();
    println!("listening on {}", listener.local_addr().unwrap());
    axum::serve(listener, app).await.unwrap();
}

3. Run it and call it#

text
cargo run

In a second terminal, curl -i http://127.0.0.1:3001/ printed:

text
HTTP/1.1 200 OK
content-type: text/plain; charset=utf-8
content-length: 15
date: Sat, 05 Dec 2026 09:02:16 GMT

hello from axum

The first terminal printed listening on 127.0.0.1:3001. Stop the server with Ctrl+C.

Check that it works#

Open http://127.0.0.1:3001/ in a browser: the page shows hello from axum. Any other path answers 404 with an empty body.

Troubleshooting#

SymptomCauseFix
the program panics at bind(...).unwrap() with an error about the address being in use (the wording differs by system)another program, or a second copy of this one, holds port 3001stop it or bind a different port
the browser cannot connect from another machine127.0.0.1 accepts only local connectionsbind 0.0.0.0:3001 when outside access is intended

Going further#

  • Next: how routing, path parameters and nested routers work (thread "How do I define routes, path parameters and nested routers in axum?").
  • Read the port and address from configuration instead of writing them in the code.

Why does axum need tokio?#

axum::serve and the handlers are async, and something has to run them. tokio is that runtime; #[tokio::main] sets it up around main.

Sources#

    QuoteReply
    Share
    Hacker NewsRedditLinkedInE-mail

    No replies yet