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"> 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳