<p>I am trying to figure out the following discrepancy in behavior for <code>http.Handle</code></p>
<p>Consider this directory structure</p>
<pre><code>myproject
static
index.html
</code></pre>
<p>And this code</p>
<pre><code>fs := http.FileServer(http.Dir("./static"))
http.Handle("/", fs)
http.ListenAndServe(":8080", nil)
</code></pre>
<p>running the above code and hitting <code>http://localhost:8080/</code> or <code>http://localhost:8080/index.html</code> I will see the <code>index.html</code> served</p>
<p>however if I do something like this</p>
<pre><code>fs := http.FileServer(http.Dir("./static"))
http.Handle("/test/", fs) // Comment: OR http.Handle("/test", fs)
http.ListenAndServe(":8080", nil)
</code></pre>
<p>running the above code and hitting <code>http://localhost:8080/test</code> or <code>http://localhost:8080/test/index.html</code> I will see 404 and no <code>index.html</code></p>
<p>I feel this is a super rookie mistake, but I just can't wrap my head around it.</p>
<p>Can someone help me out?</p>
<hr/>**评论:**<br/><br/>ROFLLOLSTER: <pre><p>Look at the second example <a href="https://golang.org/pkg/net/http/#FileServer" rel="nofollow">here</a>.</p></pre>chmikes: <pre><p>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. </p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传