英文:
Go http: difference between serving /static and /static/
问题
我对http.FileServer
和斜杠有一个很糟糕的困惑。
我需要为HTML页面提供一个脚本。在我工作的目录中,我有一个名为index.html
的页面,还有一个名为static
的目录,其中包含myscript.js
。
第一个问题:写成这样是否正确:
<script src="/static/myscript.js"></script>
?我还看到过src="static/myscript.js"
,我不知道使用其中之一是否有原因(但我猜它会影响我们在服务器上编写的处理程序)。
假设我们选择第一种版本。第二个问题:在服务器端,我想为static
目录注册处理程序。受这个例子的启发,我这样做:
fs := http.FileServer(http.Dir("./static"))
http.Handle("/static", http.StripPrefix("/static", fs))
但是我得到了一个404
错误。然而,如果我使用:
fs := http.FileServer(http.Dir("./static"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
带有结尾斜杠的方式,它可以正常工作!
我对Web服务器非常陌生,所以我会很感激任何解释,包括实际传递给函数的地址是什么。例如,我不知道(也无法从net/http
文档中找出)在提供/static
请求时传递给处理程序的地址是什么。我猜它是/static/myscript.js
,因为我们使用了http.StripPrefix
,但我没有实际的方法来证明它。
英文:
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.
答案1
得分: 0
http.Handle("/static", http.StripPrefix("/static", fs))
注册了一个固定名称模式。
http.Handle("/static/", http.StripPrefix("/static/", fs))
注册了一个根子树模式。
前者仅匹配 URL.path = "/static"
的请求。后者匹配以 "/static/"
开头的所有路径。404
表示无法匹配给定请求的任何模式,而不是请求的文件未找到。(它甚至没有执行 FileServer
的处理程序!)
回答你的第一个问题:
<script src="/static/myscript.js"></script>
以斜杠 /
开头的 URL 是绝对路径。这意味着无论你在哪个页面上,它都会始终附加到域名后面,例如 example.com/some/page
+ /static/myscript.js
= example.com/static/myscript.js
<script src="static/myscript.js"></script>
是一个相对路径。这意味着它将附加到当前访问页面的 URL 后面,例如 example.com/some/page
+ static/myscript.js
= example.com/some/page/static/myscript.js
英文:
http.Handle("/static", http.StripPrefix("/static", fs))
registers a fixed name pattern.
http.Handle("/static/", http.StripPrefix("/static/", fs))
registers a rooted subtree pattern.
The former matches only requests where URL.path = "/static"
. The latter matches every path that starts with "/static/"
. The 404
indicates that it could not match any pattern for the given request, not that the requested file wasn't found. (It does not even get to execute the FileServer
's handler!)
And to answer your first question:
<script src="/static/myscript.js"></script>
URLs starting with a slash /
are absolute. That means it does not matter on what page you are, it will always append to the domain name e.g. example.com/some/page
+ /static/myscript.js
= example.com/static/myscript.js
<script src="static/myscript.js"></script>
Is a relative path. That means it will be appended to URL of the currently visited page e.g. example.com/some/page
+ static/myscript.js
= example.com/some/page/static/myscript.js
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论