I am trying to figure out the following discrepancy in behavior for http.Handle
Consider this directory structure
myproject
static
index.html
And this code
fs := http.FileServer(http.Dir("./static"))
http.Handle("/", fs)
http.ListenAndServe(":8080", nil)
running the above code and hitting http://localhost:8080/
or http://localhost:8080/index.html
I will see the index.html
served
however if I do something like this
fs := http.FileServer(http.Dir("./static"))
http.Handle("/test/", fs) // Comment: OR http.Handle("/test", fs)
http.ListenAndServe(":8080", nil)
running the above code and hitting http://localhost:8080/test
or http://localhost:8080/test/index.html
I will see 404 and no index.html
I feel this is a super rookie mistake, but I just can't wrap my head around it.
Can someone help me out?
评论:
ROFLLOLSTER:
chmikes:Look at the second example here.
As the ROFLLOLSTER example shows, the FileServer will use the URL path to look into "./static". So it will look for "./static/test/index.html". You thus have to remove the segment of the path you don't want to be part in the file lookup.
