| Crates.io | axum_static |
| lib.rs | axum_static |
| version | 1.8.0 |
| created_at | 2023-01-22 04:47:36.194269+00 |
| updated_at | 2025-12-03 17:19:10.598379+00 |
| description | static file serving router for axum server |
| homepage | |
| repository | https://github.com/azzybana/axum_static |
| max_upload_size | |
| id | 764756 |
| size | 32,361 |
static file serving for axum
You must use axum_static that matches your axum version.
First install crate.
cargo add axum_static
Then, create a static route and nest it in the existing route like so
let app = Router::new()
.nest("/", axum_static::static_router("public"))
handle_error: Adds graceful IO error responses via tower_http's handle_error hook.mime_guess: Swaps the manual extension map for mime_guess so content-types stay current automatically.status_code: Builds on handle_error to include human-readable status text in error responses.tracing: Emits structured warn! logs for unknown MIME types and error! logs for IO failures.If your app has state, you'll need to add with_state, because static_router does not use state (()):
let app = Router::new()
.route("/", get(index))
......
.nest("/static", axum_static::static_router("static").with_state())
......
.with_state(YourAppState { ... })
The argument of the static_router function is the path to read static files based on the project root path.
Then you can read the file like this. It can also be a sub directory.

This is the end.