(Newbie) Confusion on http fileserver default path

blov · · 344 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<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(&#34;./static&#34;)) http.Handle(&#34;/&#34;, fs) http.ListenAndServe(&#34;:8080&#34;, 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(&#34;./static&#34;)) http.Handle(&#34;/test/&#34;, fs) // Comment: OR http.Handle(&#34;/test&#34;, fs) http.ListenAndServe(&#34;:8080&#34;, 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&#39;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 &#34;./static&#34;. So it will look for &#34;./static/test/index.html&#34;. You thus have to remove the segment of the path you don&#39;t want to be part in the file lookup. </p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

344 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传