expression
Axum: missing static asset responses
A missing asset normally returns 404. Preserve that distinction rather than returning a successful response for every missing file. This fixture creates an empty dedicated directory and requests one nonexistent asset. Standalone Cargo example: execute only in a fresh isolated scratch directory. Expected stdout (not executed): 404 Common mistake: Returning 200 with a generic fallback for every missing asset without considering client caching and error handling.
Formal Syntax & Structure
rust syntaxrust
ServeDir::new(path)Working Example
Example Snippetrust
use axum::{Router, body::{Body, to_bytes}, http::{Request, StatusCode}};use tower::ServiceExt;
#[tokio::main(flavor = "current_thread")]async fn main() { use tower_http::services::ServeDir; std::fs::create_dir("v73-empty-demo").unwrap(); let app = Router::new().nest_service("/assets", ServeDir::new("v73-empty-demo")); let r = app.oneshot(Request::builder().uri("/assets/missing.txt").body(Body::empty()).unwrap()).await.unwrap(); println!("{}", r.status().as_u16());}