実現したいこと
静的ファイルを読み込ませたい。(CSS, JS)
現状と困っていること
ライブラリはgorilla/mux
を使用しています。
静的ファイルがブラウザ表示したときに読み込まれません。
- ファイル構造
./front ├── handler │ └── handler.go ├── main.go(staticを読み込ませる処理を書いている) ├── static │ ├── css │ │ └── style.css │ └── js └── template ├── header.html ├── layout.html(ここで<link>を書いている) ├── post │ ├── postRegisterConfirmForm.html │ ├── postRegisterForm.html │ └── result.html └── template.go
- ソースコード
main.go
1 2(必要な箇所のみ抜粋) 3 4func main() { 5 r := mux.NewRouter() 6 7 ..... 8 9 // static フォルダの読み取り 10 http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) 11 12 svc := &http.Server{ 13 Handler: r, 14 Addr: "127.0.0.1:8080", 15 } 16 log.Fatalln(svc.ListenAndServe()) 17}
layout.html
1{{define "layout"}} 2<!DOCTYPE html> 3<html> 4<head> 5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 6 <title>Clippo</title> 7 <link rel="stylesheet" type="text/css" href="../static/css/style.css"> 8 <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> 9</head> 10<body> 11 {{template "header" . }} 12 <div class="container"> 13 {{template "content" . }} 14 </div> 15</body> 16</html> 17{{end}}
エラー内容と試したこと
ブラウザの検証画面のConsole上で、下記エラーが発生しています。
Refused to apply style from 'http://localhost:8080/static/css/style.css' because its MIME type ('text/plain') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
下記を参考にファイル構造やプログラムを書き換えましたがCSSが適応されませんでした。
お願いしたいこと
どうか解決方法を教えていただけませんか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。