dtpd58676 2018-10-01 14:19
浏览 15
已采纳

使用GO导入的Style.css出现MIME错误[重复]

This question already has an answer here:

I just started learning go and 1 of the things I really wanna learn to do is making sites in go. I watched some tutorials for it and making sites worked, but I didn't know how to add styles.

I searched for some examples on the internet and stackoverflow, but couldn't find one that actually worked for me (and stayed simple).

Underneath is the code that I ended up with. But I think I got a new problem now cause in the console it says: error

I tried a lot of solutions that I found on the internet for this but none of them worked so I am pretty sure it is because I imported the css wrongly in go.

Go (functions.go):

package main

import (
    "html/template"
    "net/http"
)

type IndexPage struct {
    Title string
    SubTitle string
}

func indexHandler(w http.ResponseWriter, r *http.Request){
    p := IndexPage{Title: "Pizza site", SubTitle: "everyone loves pizzas"} 
    t, _ := template.ParseFiles("index.html")
    t.Execute(w,p)
}

func main() {

    http.HandleFunc("/", indexHandler)
    http.Handle("/css/", http.FileServer(http.Dir("css")))
    http.ListenAndServe(":8080", nil)
}

Html (index.html):

<html lang="nl">
<head>
  <meta charset="utf-8">
  <title>Pizzaaaaaaa</title>
  <link rel="stylesheet" href="css/style.css" type="text/css">
</head>
<body>
    <article>
        <h1>
            {{ .Title }}
            <span class="subtitle">{{ .SubTitle }}</span>
        </h1>
        <p>Some text</p>
    </article>
</body>
</html>

CSS ( /css/style.css )

*{
    color: rgb(250, 157, 157);
}

FileTree

FileTree

</div>
  • 写回答

2条回答 默认 最新

  • douxiza9868 2018-10-01 14:57
    关注

    Your handle return 404 when you try to access css file from this url: /css/*

    Change your css handle with this:

        http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("./css"))))
    

    *You got 'text/plain' because the 404 is a plain text.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题