axum_static
static file serving for axum
Version
You must use axum_static that matches your axum version.
- axum 0.6 => axum_static ~1.6.*
- axum 0.7 => axum_static ~1.7.*
- axum 0.8 => axum_static ~1.8.*
Usage
First install crate.
Then, create a static route and nest it in the existing route like so
let app = new
.nest
Features
handle_error: Adds graceful IO error responses viatower_http'shandle_errorhook.mime_guess: Swaps the manual extension map formime_guessso content-types stay current automatically.status_code: Builds onhandle_errorto include human-readable status text in error responses.tracing: Emits structuredwarn!logs for unknown MIME types anderror!logs for IO failures.
State
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.