Replies: 2 comments 3 replies
-
See https://github.com/tokio-rs/axum/blob/main/examples/static-file-server/src/main.rs for lots of examples of using |
Beta Was this translation helpful? Give feedback.
0 replies
-
Unfortunately none of the examples fits well, but there is enough to get it working to answer the question: use tower_http::services::{ServeDir, ServeFile};
...
let app = Router::new()
.route(...)
.fallback_service(ServeDir::new("assets/")
.fallback(ServeFile::new("assets/index.html"))
);
axum::serve(TcpListener::bind("127.0.0.1:8080").await.unwrap(), app).await.unwrap(); This will serve all the assets from the assets folder and fall back to index.html for everything else. Fallback does not change any status,
|
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
I'm serving static-built React app (with react-router) by this code:
Axum has some api routes, and for
/
,/items/:file_name
and/search
, static files are served.This works perfectly, except that there are some
.clone()
and looks not so efficient. This is because ServeDir does not accept path with the wild card(*
).And in this case,
fallback
does not work (even when navigating in the browser correctly I encountered 404).Is there any way to solve this pattern elegantly?
axum version
0.7.2
Beta Was this translation helpful? Give feedback.
All reactions