1

I'm trying to return multiple documents (index.html, style.css) in an HTTP response when a user makes a single HTTP request. How do I go about doing this? I'm thinking using something like a std::io::File instance to return these, but this hasn't worked out so far.

The function and route below are the ones the user interacts with when visiting the website for the first time.

I have read Rocket's documentation about NamedFile and StaticFiles but I'm not so sure that these are the correct solutions in this scenario.

#[get("/")]
fn index() -> Response<'static> {
    let mut response = Response::new();
    response.set_status(Status::Ok);
    response
}
VGC3OCJA50
  • 13
  • 2
  • 6
  • This is not the proper way to serve a website, separate files should be served on separate paths. What problem are you trying to solve? If you really want a single response, you can embed the CSS file within ` – kmdreko Aug 13 '20 at 17:49
  • I'm trying to solve the problem that my CSS won't be requested by the browser. But if I include the CSS in the HTML, will the browser make an automatic request for the CSS? And if so, should I have a separate route for handling the CSS requests? – VGC3OCJA50 Aug 13 '20 at 18:56
  • 2
    yes and yes, see [here](https://www.w3schools.com/css/css_howto.asp) for how to add an external css file to your html. the `StaticFiles` handler from rocket allows you to just have your index.html and style.css in a folder and not have to worry about making all the routes explicitly – kmdreko Aug 13 '20 at 19:42
  • Ah alright I see. In that case the `StaticFiles` makes a lot more sense to use. Thanks! – VGC3OCJA50 Aug 13 '20 at 19:48

0 Answers0