-
SummaryI have a very simple Axum server I get 404 not found error for my-script.js: Problem doesn't occur when the page is served using VSCode live server. What am i doing wrong? Thanks! === src/main.rs === async fn main() -> Result<()> {
// Create top level router
let routes_all = Router::new()
.route("/", get(root))
.nest_service("/home", get_service(ServeDir::new("assets"))); // <<------
let api_server_addr = "localhost:3000";
// Create a TCP listener
let listener = tokio::net::TcpListener::bind(api_server_addr).await?;
// Start serving the app
println!("API server listening on: {api_server_addr}");
axum::serve(listener, routes_all).await?;
Ok(()) // main() completed with no errors
}
// endregion: ---- MAIN ----
async fn root() -> Html<String> {
Html("<h1> Hello from the root route</h1>".to_string())
} === assets/index.html === <!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
<meta charset='utf-8'>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<p>Hello from index.html</p>
<script src="my-script.js"></script> <!-- Cant find this file -->
</body>
</html> axum versionaxum = "0.7.5", tower-http = {version = "0.5.2", features = ["fs"]} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
How are you testing this? If you're opening the page in a browser at |
Beta Was this translation helpful? Give feedback.
I suspect this could be another case of the
/home/
vs/home
nesting issue.When you called
/home
, the directory is/
so the browser tried to fetch/my-script.js
. If you had usednest_service("/home/", ...);
and calledlocalhost/home/
instead, I think it would correctly use the relative path as/home/my-script.js
.You could try calling
locahost/home/index.html
, I think that version should work. I didn't try to run any code though, so I'm just extrapolating and guessing here.