为什么我需要使用http.StripPrefix来访问静态文件?
梅因,加油 目录结构:为什么我需要使用http.StripPrefix来访问静态文件?,http,go,url-rewriting,server,url-routing,Http,Go,Url Rewriting,Server,Url Routing,梅因,加油 目录结构: package main import ( "net/http" ) func main() { http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) http.ListenAndServe(":8080", nil) } 即使在阅读了文档之后,我也很难理解http.StripPrefix在这里到底做了什么 1) 如
package main
import (
"net/http"
)
func main() {
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
http.ListenAndServe(":8080", nil)
}
即使在阅读了文档之后,我也很难理解http.StripPrefix
在这里到底做了什么
1) 如果删除http.StripPrefix
,为什么不能访问localhost:8080/static
2) 如果我删除该函数,哪个URL映射到/static
文件夹?将请求处理转发给指定为其参数的URL,但在此之前,它通过去掉指定的前缀来修改请求URL
例如,如果浏览器(或HTTP客户端)请求资源:
%GOPATH%/src/project_name/main.go
%GOPATH%/src/project_name/static/..files and folders ..
StripPrefix
将剪切/static/
,并将修改后的请求转发给返回的处理程序,这样它将看到请求的资源已被删除
/static/example.txt
http.FileServer()
返回的处理程序
将查找并提供与指定为其参数的文件夹(或者更确切地说是文件系统
)相关的文件内容(您指定了“static”
作为静态文件的根)
现在,由于“example.txt”
位于static
文件夹中,因此必须指定该文件夹的相对路径才能获得相应的文件路径
另一个例子
此示例可以在http包文档页()上找到:
说明:
FileServer()
被告知静态文件的根是“/tmp”
。我们希望URL以“/tmpfiles/”
开头。因此,如果有人请求“/tempfiles/example.txt”
,我们希望服务器发送文件“/tmp/example.txt”
为了实现这一点,我们必须从URL中删除“/tmpfiles”
,剩下的将是与根文件夹“/tmp”
相比的相对路径,如果我们加入该文件夹,将给出:
// 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"))))
我将逐一回答这两个问题 问题1的答案1: 如果您的代码编写如下:
/tmp/example.txt
您的根文件夹是%GOPATH%/src/project\u name/static/
。当您访问localhost:8080/static
时,URL/static
将转发给返回的处理程序。但是,在根文件夹中没有名为static
的目录或文件
问题2的答案2:通常,如果您删除了该文件夹,则无法访问/static
文件夹。但是,如果您有一个名为static
的目录或文件,您可以使用URLlocalhost:8080:/static
访问它(完全不是您想要的根目录)
顺便说一句,如果您的URL不是以/static
开头,您将无法访问任何内容,因为http.ServeMux
不会重定向您的请求。假设
我有一份档案
http.Handle("/static/", http.FileServer(http.Dir("static"))
然后,告诉fileserve服务于本地目录
/home/go/src/js/kor.js
示例1-Filerserver根目录的根url
现在,文件服务器将“/”
请求作为“/home/go/src/js”+“/”
是,http://localhost/kor.js
request告诉文件服务器,在中查找kor.js
http.Handle("/", fs)
http.Handle("/static/", fs)
我们得到了kor.js
文件
示例2-文件服务器根目录的自定义url
但是,如果我们添加额外的resquest名称
"/home/go/src/js" + "/" + "kor.js".
现在,文件服务器将“/static/”
请求作为“/home/go/src/js”+“/static/”
是,http://localhost/static/kor.js
request告诉文件服务器,在中查找kor.js
http.Handle("/", fs)
http.Handle("/static/", fs)
我们得到404错误
示例3-文件服务器根目录的自定义url
所以,在文件服务器使用http.StripPrefix(“/tmpfiles/”,…
在stripPrefix
之后,文件服务器使用/
代替/static/
"/home/go/src/js" + "/static/" + "kor.js".
获取
kor.js
对于任何子目录有问题的人,您需要添加一个“
,因为它似乎仅当路径是二进制文件根目录中的文件夹时才将其视为相对路径
比如说
"/home/go/src/js" + "/" + "kor.js".
注意“
/uploads
”之前的“”
您是否尝试了http.Handle(“/static/”,http.FileServer(http.Dir(“/”))?http.Handle(“/static/”,http.FileServer(http.Dir(“”))
有效。对于任何阅读本文的人,请确保您在/tmpfiles/
中有结尾斜杠,否则您将度过一段不愉快的时光。为了澄清和传播正确的常识,句柄中的/tmpfile/
很重要,但在StripPrefix中不重要,比如说http.Handle(“/tmpfiles/”,http.StripPrefix(/tmpfiles),http.FileServer(http.Dir(“/tmp”)
也会起作用
s.router.PathPrefix("/logos/").Handler(
http.StripPrefix("/logos/", http.FileServer(http.Dir("./uploads/logos/"))))