drnbotxo449747 2018-02-13 02:37
浏览 34
已采纳

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

In my root handle ("/") or clients handle ("/clients") the static files are correctly avaliable, and looking the network tab on chrome, i see the server request like this:

localhost:8080/static/file.example

But if i'm on the secondary handle ("/Clients/route"), don't work correctly, i see this:

localhost:8080/clients/static/file.example

The StripPrefix don't remove the "client" from the request.

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)

-

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

-

File tree:

tree

  • 写回答

1条回答 默认 最新

  • douzhi1879 2018-02-13 04:27
    关注

    This isn't an issue with the handler, it is an issue with your HTML link.

    <link rel="stylesheet" href="static/leaflet/leaflet.css" /> is relative to the current URL.

    That is to say, static/leaflet/leaflet.css will be appended to the current URL - when you are on your homepage this isn't an issue as it translates to /static/leaflet/leaflet.css but when you are on your clients page it turns into /clients/static/leaflet/leaflet.css.

    The simple fix is just to add a leading / to your href:

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

    This makes the URL absolute and it will be unaffected by visiting other pages of your website.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!