iron / staticfile

Static file-serving middleware for the Iron web framework.
MIT License
63 stars 56 forks source link

How serve 404 into static file for example index.html or 404.html #62

Open Alsmile opened 8 years ago

Alsmile commented 8 years ago

mount.mount("/", staticfile::Static::new(Path::new("../assets/index.html")));

I hope it serve the static file when the Path is file. (fn Path::is_file(&self) -> bool) / => index.html /404 => index.html

more : https://github.com/rackt/react-router/blob/latest/docs/guides/basics/Histories.md#configuring-your-server

dashed commented 8 years ago

I'm wondering how to do this as well.

dashed commented 8 years ago

@Alsmile This may interest you.


I glanced at the source code of staticfile and used the following, in my opinion, hacky approach; which was adapted from https://github.com/iron/router/blob/master/examples/custom_404.rs

This works for my js app that uses html5 (pop/push)state:

struct Custom404 {
    staticfile: Static
}

impl AfterMiddleware for Custom404 {
    fn catch(&self, req: &mut Request, err: IronError) -> IronResult<Response> {

        // TODO: so hacky. need better alternative
        req.url.path = vec!["index.html".to_string()];

        return self.staticfile.handle(req);
        // return Ok(Response::with((status::NotFound, "404 Not Found")));
    }
}

// ...

    let custom_404 = Custom404 {
        staticfile: Static::new(app_path)
    };

    log_chain.link_after(custom_404);