0

I coded the file server in golang as follows

func (s *Server) Start() {
    s.Mux.PathPrefix("/public/").Handler(http.StripPrefix("/public/", http.FileServer(http.Dir("public")))) // 파일 등록

After that, i'm connect with a browser to ' localhost/public/temp.json ' The file returns fine, but I think something is wrong, Because the url is localhost/public/temp.json

because stripPrefix is remove 'public', must be url is ' localhost/temp.json '

What's wrong? I am learning golang, can you help me?

madol
  • 551
  • 2
  • 17
  • http.StripPrefix just StripPrefix the filepath but url : if you don't StripPrefix `localhost/public/temp.json` ->wil find the path `localhost/public/public/temp.json`,if you want `localhost/temp.json` set `s.Mux.PathPrefix("/")` – Para Nov 16 '22 at 03:04
  • Even if I apply the following, the same symptom. ' s.Mux.PathPrefix("/").Handler(http.StripPrefix("/public/", http.FileServer(http.Dir("public/")))) ' – madol Nov 16 '22 at 03:56
  • 1
    `StripPrefix` is for stripping the prefix of the URL's **path**, not the URL's host. If the *path* was `/localhost/public/temp.json` then `StripPrefix("/public/", ...)` would not strip *anything* since that path starts with `/localhost...` and not `/public...`. – mkopriva Nov 16 '22 at 04:53
  • I'm a beginner, I don't understand, I want to import a file while hiding the path, can you show me a simple example? sorry – madol Nov 16 '22 at 07:39

0 Answers0