static file serving router for axum server
Usage is very simple.
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()
.route("/", get(index))
......
.nest("/static", axum_static::static_router("static"))
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.
How easy is it?