Skip to content

How to serve a static directory? #129

Answered by kaplanelad
flappyBug asked this question in Q&A
Discussion options

You must be logged in to vote

Hey @flappyBug

you can implement the after_routes hook.
The hook provides access to the Axum router, making both the nest_service and fallback_service available for your use.

For example:

pub struct App;
#[async_trait]
impl Hooks for App {
 ....

fn after_routes(router: AxumRouter, _ctx: &AppContext) -> Result<AxumRouter> {
        let router = router.nest_service("/assets", get_service(ServeDir::new("dist/assets")))
        .fallback_service(get_service(ServeFile::new("dist/index.html")));
        Ok(router)
    }
....
}

Replies: 4 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by flappyBug
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants