英文:
Can not call embed css/js files
问题
我正在嵌入我的CSS和JS文件,代码如下所示:
package resources
import (
"embed"
)
// WebUI 是我们从 Onsen UI 导入的静态 Web UI。
//go:embed public/onsen
var WebUI embed.FS
// Views 是我们的静态 Web 服务器布局、带有动态内容的视图和静态视图的部分内容。
//go:embed templates/layouts templates/views templates/partials
var Views embed.FS
并尝试在我的主函数中将 WebUI
定义为 static
文件夹:
package main
import (
"fmt"
"log"
"html/template"
"net/http"
"onsen/resources"
)
var view *template.Template
var err error
func init() {
fmt.Println("Starting up.")
view = template.Must(template.ParseFS(resources.Views, "templates/layouts/*.html", "templates/views/*.html", "templates/partials/*.html"))
if err != nil {
log.Fatal("Error loading templates:" + err.Error())
}
}
func main() {
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(resources.WebUI))))
//http.Handle("/static/", http.FileServer(http.FS(resources.WebUI)))
http.HandleFunc("/index", index)
server := http.Server{
Addr: "127.0.0.1:8070",
}
server.ListenAndServe()
}
func index(w http.ResponseWriter, r *http.Request) {
err = view.ExecuteTemplate(w, "index.html", nil)
if err != nil {
log.Fatalln(err)
}
}
文件夹结构如下所示:
但是在运行时,我遇到了以下错误:
CSS 文件:拒绝应用样式从 'http://127.0.0.1:8070/static/css/onsenui.css',因为它的 MIME 类型 ('text/plain') 不是受支持的样式表 MIME 类型,并且启用了严格的 MIME 检查。
JS 文件:加载资源失败:服务器响应状态为 404 (未找到)
英文:
I'm embedding my css and js files as below:
package resources
import (
"embed"
)
// WebUI is our static web ui from onsen ui.
//go:embed public/onsen
var WebUI embed.FS
// Views is our static web server layouts, views with dynamic content and partials content that is a static view.
//go:embed templates/layouts templates/views templates/partials
var Views embed.FS
And trying to define this WebUI
as static
folder in my main function:
package main
import (
"fmt"
"log"
"html/template"
"net/http"
"onsen/resources"
)
var view *template.Template
var err error
func init() {
fmt.Println("Starting up.")
view = template.Must(template.ParseFS(resources.Views, "templates/layouts/*.html", "templates/views/*.html", "templates/partials/*.html"))
if err != nil {
log.Fatal("Error loading templates:" + err.Error())
}
}
func main() {
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(resources.WebUI))))
//http.Handle("/static/", http.FileServer(http.FS(resources.WebUI)))
http.HandleFunc("/index", index)
server := http.Server{
Addr: "127.0.0.1:8070",
}
server.ListenAndServe()
}
func index(w http.ResponseWriter, r *http.Request) {
err = view.ExecuteTemplate(w, "index.html", nil)
if err != nil {
log.Fatalln(err)
}
}
The folders structure is as shown:
But a running, I' getting the below errors:
CSS files: Refused to apply style from 'http://127.0.0.1:8070/static/css/onsenui.css' because its MIME type ('text/plain') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
JS files: Failed to load resource: the server responded with a status of 404 (Not Found)
答案1
得分: 0
我通过修复static
路由来解决了这个问题,代码如下:
http.Handle("/webUI/", http.StripPrefix("/webUI/", http.FileServer(http.FS(resources.WebUI))))
同时,css
和js
文件的调用如下:
<link rel="stylesheet" type="text/css" href="/webUI/public/onsen/css/onsenui.css"/>
即:
'静态路径[/webUI] + 原始路径[public/onsen/...]
英文:
I solved it by fixing the static
route as:
http.Handle("/webUI/", http.StripPrefix("/webUI/", http.FileServer(http.FS(resources.WebUI))))
And both css
and js
files calls as:
<link rel="stylesheet" type="text/css" href="/webUI/public/onsen/css/onsenui.css"/>
that is:
'The static path [/webUI] + the original route [public/onsen/...]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论