无法调用嵌入的 CSS/JS 文件。

huangapple go评论87阅读模式
英文:

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/JS 文件。

但是在运行时,我遇到了以下错误:

无法调用嵌入的 CSS/JS 文件。

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:

无法调用嵌入的 CSS/JS 文件。

But a running, I' getting the below errors:

无法调用嵌入的 CSS/JS 文件。

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))))

同时,cssjs文件的调用如下:

<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(&quot;/webUI/&quot;, http.StripPrefix(&quot;/webUI/&quot;, http.FileServer(http.FS(resources.WebUI))))

And both css and js files calls as:

&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;/webUI/public/onsen/css/onsenui.css&quot;/&gt;

that is:
&#39;The static path [/webUI] + the original route [public/onsen/...]

huangapple
  • 本文由 发表于 2021年10月23日 02:59:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/69681914.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定