在二级路由(“route/secondary/route”)Golang中提供静态文件

在二级路由(“route/secondary/route”)Golang中提供静态文件,go,Go,在我的根句柄(“/”)或客户端句柄(“/clients”)中,静态文件可以正确访问,查看chrome上的网络选项卡,我看到服务器请求如下所示: localhost:8080/static/file.example 但是如果我在第二个句柄(“/Clients/route”)上,工作不正常,我会看到: localhost:8080/clients/static/file.example StripPrefix不会从请求中删除“客户端” func main() { http.Handle("/st

在我的根句柄(“/”)或客户端句柄(“/clients”)中,静态文件可以正确访问,查看chrome上的网络选项卡,我看到服务器请求如下所示:

localhost:8080/static/file.example
但是如果我在第二个句柄(“/Clients/route”)上,工作不正常,我会看到:

localhost:8080/clients/static/file.example
StripPrefix不会从请求中删除“客户端”

func main() {
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("/static"))))

http.HandleFunc("/", index)
http.HandleFunc("/clients", controllers.MostrarClientes)
http.HandleFunc("/clientes/route", controllers.MainIndex)

http.ListenAndServe(":8080", nil)
-


-

文件树:


这不是处理程序的问题,而是HTML链接的问题

是相对于当前URL的

也就是说,
静态/传单/传单.css
将附加到当前URL中-当您在主页上时,这不是一个问题,因为它会转换为
/static/sloop/sloop.css
,但当您在客户端页面上时,它会变成
/clients/static/sloop/sloop.css

简单的修复方法就是在
href
中添加一个前导的
/

这使得URL是绝对的,并且不会受到访问网站其他页面的影响

<link rel="stylesheet" href="static/leaflet/leaflet.css" />