drjgk44268 2018-08-16 08:44
浏览 17
已采纳

处理网址“ / foobar /”会替换css <link>,而js <script>路径以“ / foobar /”开头

I'm trying to use the standard Go http package for my router.

In my main.go it starts:

func main() {
    mux := http.NewServeMux()

    fs := http.FileServer(http.Dir("static"))
    handler := http.StripPrefix("/static/", fs)
    mux.Handle("/static/", handler)

    mux.HandleFunc("/my-example-url/", FooHandler)
}

inside FooHandler() I have some println()

func FooHandler(w http.ResponseWriter, r *http.Request) {
    println("r.URL.Path->",r.URL.Path)
    //more business logic
}

// output:

r.URL.Path-> /my-example-url/static/css/normalize.css
r.URL.Path-> /my-example-url/static/libs/xss.js

So the initial part of url should NOT be there (the /my-example-url/ part)

I think this only happends when I'm trying to serve an endpoint with trailing slash such as:

mux.Handle("/my-example-url/", handler)

My end goal is to get some resource based on the id I'm trying to pass in the url after the trailing slash for example:

http://localhost:3001/my-example-url/bb98610
  • 写回答

1条回答 默认 最新

  • dongzhong6675 2018-08-16 09:13
    关注

    In the html file, the one that's triggering the requests for the static resources, you are most probably using relative paths instead of absolute ones which causes the browser to append that relative path to the path already in the location bar.

    For example:

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

    will be turned, by the browser, into /my-example-url/static/css/normalize.css.

    Instead you want to use (note the leading slash):

    <link href="/static/css/normalize.css" rel="stylesheet"> 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效