I have a terrible confusion concerning http.FileServer
and slashes.
I need to serve a script to a html page. In the directory I'm working I have the page index.html
and I have a static
directory with myscript.js
inside of it.
First question: is it correct to write
<script src="/static/myscript.js"></script>
? I have also seen src="static/myscript.js"
and I don't know if there is a reason for using one or the other (but I guess it influences the handler we have to write on the server).
Let's suppose we settle for the first version. Second question: on the server side, I want to register the handler for directory static
. Inspired by this example, I do:
fs := http.FileServer(http.Dir("./static"))
http.Handle("/static", http.StripPrefix("/static", fs))
but I get a 404
. However, if I use:
fs := http.FileServer(http.Dir("./static"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
with the ending slashes, it works fine!
I'm really new to web servers, so I would appreciate any explanation that includes what are the actual addresses passed around by functions. For example, I don't know (and I can't figure it out from the net/http
documentation ) what is the address that is passed to the handler when serving a /static
request. I guess it's /static/myscript.js
since we are using http.StripPrefix
but I have no actual way of proving it.