Building Web Servers with Axum
In a previous post, we learned about asynchronous programming with Tokio. This time, we are going to use Axum to build a server that uses Tokio as runtime.
Hello world
Creating a simple server with Axum only requires a few lines:
1
2
3
4
5
6
#[tokio::main]
async fn main() {
let app = Router::new().route("/", get(|| async { "Hello, World!" }));
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, app).await.unwrap();
}
If we run this code, we will see a Hello, World!
message if we visit http://localhost:3000
.