Go语言中,关于服务静态文件的两种方式`/static`和`/static/`之间有什么区别?

huangapple go评论72阅读模式
英文:

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

&lt;script src=&quot;/static/myscript.js&quot;&gt;&lt;/script&gt;

? I have also seen src=&quot;static/myscript.js&quot; 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(&quot;./static&quot;))
http.Handle(&quot;/static&quot;, http.StripPrefix(&quot;/static&quot;, fs))

but I get a 404. However, if I use:

fs := http.FileServer(http.Dir(&quot;./static&quot;))
http.Handle(&quot;/static/&quot;, http.StripPrefix(&quot;/static/&quot;, 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(&quot;/static&quot;, http.StripPrefix(&quot;/static&quot;, fs)) registers a fixed name pattern.

http.Handle(&quot;/static/&quot;, http.StripPrefix(&quot;/static/&quot;, fs)) registers a rooted subtree pattern.

The former matches only requests where URL.path = &quot;/static&quot;. The latter matches every path that starts with &quot;/static/&quot;. 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:

&lt;script src=&quot;/static/myscript.js&quot;&gt;&lt;/script&gt;

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

&lt;script src=&quot;static/myscript.js&quot;&gt;&lt;/script&gt;

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

huangapple
  • 本文由 发表于 2016年3月4日 03:44:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/35781571.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定