I am trying to serve some fonts, but when i visit localhost:4000/fonts, it gives me 404 not found. my code:
fs := http.FileServer(http.Dir("./fonts")) http.Handle("/fonts", http.StripPrefix("/fonts/", fs)) http.Handle("/", app.routes()) log.Println("Serving at localhost:4000...") log.Fatal(http.ListenAndServe(fmt.Sprintf("localhost:%d", cfg.Port), nil))
if im serving from "/" and not "/fonts", it works. but i want it to work from "/fonts".
You should not strip the trailing slash after fonts since you want the result to be /file and not file.
You should also add the trailing slash to the handler path, for the same reason.
http.Handle("/fonts/", http.StripPrefix("/fonts", fs))
Both changes, as mentioned, have the purpose to leave you with a path like
/somefile that is looked up against the filesevers file system.
Answered By – The Fool
Answer Checked By – David Marino (GoLangFix Volunteer)