CSS文件正在以Content-Type: text/plain发送。

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

CSS files are being sent with Content-Type: text/plain

问题

我看了一下,没有找到我想要的具体内容。我正在使用Go的标准库构建一个API。当提供我的资源文件时,我的CSS文件被发送为text/plain,而我希望将其发送为text/css

控制台向我抛出一个信息提示:

Resource interpreted as Stylesheet but transferred with MIME type text/plain: "http://localhost:3000/css/app.css".

main.go

package main

import (
	"bufio"
	"log"
	"net/http"
	"os"
	"strings"
	"text/template"
)

func main() {
	templates := populateTemplates()

	http.HandleFunc("/",
		func(w http.ResponseWriter, req *http.Request) {
			requestedFile := req.URL.Path[1:]
			template := templates.Lookup(requestedFile + ".html")

			if template != nil {
				template.Execute(w, nil)
			} else {
				w.WriteHeader(404)
			}
		})

	http.HandleFunc("/img/", serveResource)
	http.HandleFunc("/css/", serveResource)

	log.Fatal(http.ListenAndServe(":3000", nil))
}

func serveResource(w http.ResponseWriter, req *http.Request) {
	path := "public" + req.URL.Path
	var contentType string
	if strings.HasSuffix(path, ".css") {
		contentType = "text/css"
	} else if strings.HasSuffix(path, ".png") {
		contentType = "image/png"
	} else {
		contentType = "text/plain"
	}

	f, err := os.Open(path)

	if err == nil {
		defer f.Close()
		w.Header().Add("Content Type", contentType)

		br := bufio.NewReader(f)
		br.WriteTo(w)
	} else {
		w.WriteHeader(404)
	}
}

func populateTemplates() *template.Template {
	result := template.New("templates")

	basePath := "templates"
	templateFolder, _ := os.Open(basePath)
	defer templateFolder.Close()

	templatePathRaw, _ := templateFolder.Readdir(-1)

	templatePaths := new([]string)
	for _, pathInfo := range templatePathRaw {
		if !pathInfo.IsDir() {
			*templatePaths = append(*templatePaths,
				basePath+"/"+pathInfo.Name())
		}
	}

	result.ParseFiles(*templatePaths...)

	return result
}

我相信我正在发送text/css。我看错了吗?

英文:

I have looked around and haven't found anything specific for what I am after. I am building an API using Go's standard library. When serving up my resource files, my CSS files are sent as text/plain when I am wanting to send it as text/css.

The console throws an info message at me saying:

Resource interpreted as Stylesheet but transferred with MIME type text/plain: "http://localhost:3000/css/app.css".

main.go

package main

import (
	"bufio"
	"log"
	"net/http"
	"os"
	"strings"
	"text/template"
)

func main() {
	templates := populateTemplates()

	http.HandleFunc("/",
		func(w http.ResponseWriter, req *http.Request) {
			requestedFile := req.URL.Path[1:]
			template := templates.Lookup(requestedFile + ".html")

			if template != nil {
				template.Execute(w, nil)
			} else {
				w.WriteHeader(404)
			}
		})

	http.HandleFunc("/img/", serveResource)
	http.HandleFunc("/css/", serveResource)

	log.Fatal(http.ListenAndServe(":3000", nil))
}

func serveResource(w http.ResponseWriter, req *http.Request) {
	path := "public" + req.URL.Path
	var contentType string
	if strings.HasSuffix(path, ".css") {
		contentType = "text/css"
	} else if strings.HasSuffix(path, ".png") {
		contentType = "image/png"
	} else {
		contentType = "text/plain"
	}

	f, err := os.Open(path)

	if err == nil {
		defer f.Close()
		w.Header().Add("Content Type", contentType)

		br := bufio.NewReader(f)
		br.WriteTo(w)
	} else {
		w.WriteHeader(404)
	}
}

func populateTemplates() *template.Template {
	result := template.New("templates")

	basePath := "templates"
	templateFolder, _ := os.Open(basePath)
	defer templateFolder.Close()

	templatePathRaw, _ := templateFolder.Readdir(-1)

	templatePaths := new([]string)
	for _, pathInfo := range templatePathRaw {
		if !pathInfo.IsDir() {
			*templatePaths = append(*templatePaths,
				basePath+"/"+pathInfo.Name())
		}
	}

	result.ParseFiles(*templatePaths...)

	return result
}

I believe I am sending it as text/css. Am I seeing this wrong?

答案1

得分: 7

应用程序在内容类型标头名称中缺少一个“-”。将代码更改为:

    w.Header().Add("Content-Type", contentType)
英文:

The application is missing a "-" in the content type header name. Change the code to:

    w.Header().Add("Content-Type", contentType)

huangapple
  • 本文由 发表于 2016年3月31日 10:44:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/36323232.html
匿名

发表评论

匿名网友

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

确定