1

I'm using Rocket multiple segments to serve static files, by this code:

#![feature(proc_macro_hygiene, decl_macro)]

#[macro_use] extern crate rocket;

use std::path::PathBuf;
use rocket::response::NamedFile;
use std::path::Path;

#[get("/public/<file..>")]
fn files(file: PathBuf) -> Option<NamedFile> {
    NamedFile::open(Path::new("static/").join(file)).ok()
}

fn main() {
    rocket::ignite()
        .mount("/public", routes![files])
        .launch();
}

With the above server, requests for files at /public/<path..> should be handled by returning the contents of /static/<path..>, right?

Then on my browser, I enter the domain name along with /public/ and a multipe-segment route path, for example, a *.jpg file, but it cannot serve the file:

404 error

As far as I check, the JPG file exists. I cannot figure out what I'm missing!

UPDATE

I also tried to use a builtin module to do so:

#![feature(proc_macro_hygiene, decl_macro)]

#[macro_use] extern crate rocket;

use rocket_contrib::serve::StaticFiles;

fn main() {
    rocket::ignite()
        .mount("/public", StaticFiles::from("/static"))
        .launch();
}

But it's not working either. Not sure why!

Megidd
  • 7,089
  • 6
  • 65
  • 142

1 Answers1

1

To resolve:

  1. Notice the absolute vs relative path difference: .mount("/", StaticFiles::from("static")) means static folder should be next to the server executable.
  2. Run the server executable in the proper working directory and place static folder next to it.
Megidd
  • 7,089
  • 6
  • 65
  • 142