1

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))

UPDATE

if im serving from "/" and not "/fonts", it works. but i want it to work from "/fonts".

1
  • i cant believe im the only one in the world encountering this problem, since no answers XD Apr 16, 2022 at 10:58

1 Answer 1

1

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.