dongqing220586 2015-01-14 14:30
浏览 343
已采纳

为什么需要使用http.StripPrefix访问我的静态文件?

main.go

package main

import (
    "net/http"
)

func main() {
    http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
    http.ListenAndServe(":8080", nil)
}

Directory structure:

%GOPATH%/src/project_name/main.go
%GOPATH%/src/project_name/static/..files and folders ..

Even after reading the documentation I have trouble understanding what exactly http.StripPrefix does here.

1) Why can't I access localhost:8080/static if I remove http.StripPrefix?

2) What URL maps to /static folder if I remove that function?

  • 写回答

3条回答 默认 最新

  • dongnuochen9449 2015-01-14 15:10
    关注

    http.StripPrefix() forwards the handling of the request to one you specify as its parameter, but before that it modifies the request URL by stripping off the specified prefix.

    So for example in your case if the browser (or an HTTP client) requests the resource:

    /static/example.txt
    

    StripPrefix will cut the /static/ and forward the modified request to the handler returned by http.FileServer() so it will see that the requested resource is

    /example.txt
    

    The Handler returned by http.FileServer() will look for and serve the content of a file relative to the folder (or rather FileSystem) specified as its parameter (you specified "static" to be the root of static files).

    Now since "example.txt" is in the static folder, you have to specify a relative path to that to get the corrent file path.

    Another Example

    This example can be found on the http package documentation page (here):

    // To serve a directory on disk (/tmp) under an alternate URL
    // path (/tmpfiles/), use StripPrefix to modify the request
    // URL's path before the FileServer sees it:
    http.Handle("/tmpfiles/",
            http.StripPrefix("/tmpfiles/", http.FileServer(http.Dir("/tmp"))))
    

    Explanation:

    FileServer() is told the root of static files is "/tmp". We want the URL to start with "/tmpfiles/". So if someone requests "/tempfiles/example.txt", we want the server to send the file "/tmp/example.txt".

    In order to achieve this, we have to strip "/tmpfiles" from the URL, and the remaining will be the relative path compared to the root folder "/tmp" which if we join gives:

    /tmp/example.txt
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突