当前位置:首页 > 文章列表
>
Golang >
Go问答 >
处理 url "/foobar/" 替换 css 和 js ,这篇文章主要会讲到等等知识点,如果你在看完本篇文章后,有更好的建议或者发现哪里有问题,希望大家都能积极评论指出,谢谢!希望我们能一起加油进步!
问题内容
我正在尝试为我的路由器使用标准 go http 包。
在我的 main.go 中它开始:
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)
}
在 foohandler() 里面我有一些 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
所以 url 的初始部分不应该在那里(/my-example-url/ 部分)
我认为这只在我尝试为带有尾部斜杠的端点提供服务时才会发生,例如:
mux.handle("/my-example-url/", handler)
我的最终目标是根据我试图在尾部斜杠后面传递的 url 获取一些资源,例如:
http://localhost:3001/my-example-url/bb98610
解决方案
在触发静态资源请求的 html 文件中,您很可能使用相对路径而不是绝对路径,这会导致浏览器将该相对路径附加到位置栏中已有的路径。 p>
例如:
将被浏览器转换为 /my-example-url/static/css/normalize.css。
相反,您想使用(注意前导斜杠):
好了,本文到此结束,带大家了解了《处理 url "/foobar/" 替换 css 和 js
版本声明
本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
win7系统电脑老是自动关机解决方法
- 上一篇
- win7系统电脑老是自动关机解决方法
- 下一篇
- 如何从任何结构类型派生结构列表 - 从接口{}到可变长度切片接口{}
查看更多
最新文章
-
- Golang · Go问答 | 7小时前 |
- Go cgo.Handle 管理 Go 值跨语言传递
- 403浏览 收藏
-
- Golang · Go问答 | 7小时前 | CGO · 内存管理 · Go问答 · Go CGO unsafe.Pointer cgo.Handle runtime.Pinner
- Go cgo 指针规则导致 panic 的边界定位
- 223浏览 收藏
-
- Golang · Go问答 | 8小时前 | go ·
- Go GOMAXPROCS 变化对并发吞吐的影响
- 417浏览 收藏
-
- Golang · Go问答 | 9小时前 | go · pprof · Go alloc_space inuse_space heap profile
- Go heap profile inuse_space 与 alloc_space 的选择
- 350浏览 收藏
-
- Golang · Go问答 | 9小时前 | go · 性能排查 ·
- Go runtime/trace 观察阻塞区域的阅读方式
- 283浏览 收藏
-
- Golang · Go问答 | 9小时前 | Go问答 · Go pprof 性能剖析 goroutine泄漏 并发排查
- Go pprof goroutine 泄漏剖析的采样方法
- 104浏览 收藏
-
- Golang · Go问答 | 10小时前 | 垃圾回收 · 资源管理 · Go问答 · Go 资源释放 AddCleanup close SetFinalizer
- Go SetFinalizer 不适合资源关闭的替代方案
- 439浏览 收藏
-
- Golang · Go问答 | 10小时前 |
- Go runtime.KeepAlive 防止句柄过早回收
- 489浏览 收藏
-
- Golang · Go问答 | 10小时前 |
- Go Signal Stop 与退出流程的竞态处理
- 168浏览 收藏
-
- Golang · Go问答 | 2天前 |
- Go ColumnTypes 推断动态查询字段的安全用法
- 319浏览 收藏
