【发布时间】:2018-08-18 07:39:52
【问题描述】:
我无法获取放置在静态文件夹中的文件。我正在使用 gorilla mux 包。
main.go 代码:
fs := http.FileServer(http.Dir("static"))
mainRouter.PathPrefix("/static/").Handler(http.StripPrefix("/static/", fs))
http.Handle("/", &mainRouter)
项目结构:
static
templates
--style
--javascript
--...
main.go
当我点击索引页面时:
本地主机:8080/cruise_schedule
我得到了所有样式表和 js 文件,但是当我跳转到另一个页面时:
localhost:8080/cruise_schedule/selected_cruise/e58ed042aad24b9b87fba8917c085534
我收到以下错误:
Refused to apply style from 'http://localhost:8080/cruise_schedule/static/style/style.css' because its MIME type ('text/plain') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
和:
http://localhost:8080/cruise_schedule/static/javascript/resources.js
我应该怎么做才能正确地提供静态文件?
【问题讨论】:
-
服务很好(很明显,因为它有效)。问题是您在 HTML 中使用了相对路径,而您应该使用绝对路径(绝对路径以
/开头)。
标签: go static-files