12 个版本 (稳定)
1.7.1 | 2024年4月29日 |
---|---|
1.7.0 | 2024年4月24日 |
1.2.3 | 2023年10月24日 |
1.2.2 | 2023年7月16日 |
0.1.2 | 2023年1月22日 |
#1149 在 HTTP 服务器
每月下载量 MIT 许可证
8KB
104 代码行
axum_static
为 axum 提供静态文件服务
版本
您必须使用与您的 axum 版本匹配的 axum_static。
- axum 0.6 => axum_static ~1.6.*
- axum 0.7 => axum_static ~1.7.*
用法
首先安装 crate。
cargo add axum_static
然后,创建一个静态路由并将其嵌套在现有路由中,如下所示
let app = Router::new()
.nest("/", axum_static::static_router("public"))
如果您的应用程序具有状态,则需要添加 with_state
,因为 static_router 不使用状态 (()
)
let app = Router::new()
.route("/", get(index))
......
.nest("/static", axum_static::static_router("static").with_state())
......
.with_state(YourAppState { ... })
static_router 函数的参数是基于项目根路径读取静态文件的路径。
然后您可以像这样读取文件。它也可以是子目录。
这就是结束。
依赖项
~6.5–8.5MB
~150K SLoC