使用Go服务器时,Mime类型不正确。

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

MIme types not correct when using Go server

问题

我正在学习如何使用Go创建后端,并且我创建了一个简单的服务器,只加载一个登录页面。当页面加载时,外部的CSS和JS文件没有被加载。当我查看控制台以查看问题时,我得到了这个消息:"Resource interpreted as Stylesheet but transferred with MIME type text/html: http://localhost:8081/login/css/bootstrap.min.css"。我在HTML文件中包含的所有CSS和JS文件都出现了这个错误。

以下是Go代码:

package main

import (
    "net/http"
)

func loginFunc(w http.ResponseWriter, r *http.Request) {
    http.ServeFile(w, r, "index.html")
}

func main() {
    http.HandleFunc("/login/", loginFunc)
    http.ListenAndServe(":8081", nil)
}

以下是HTML代码:

<!DOCTYPE HTML>
<html>
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">

        <script type="text/javascript" src="js/jquery-2.1.1.min.js"></script>
        <script type="text/javascript" src="js/bootstrap.min.js"></script>

        <style type="text/css">
            .topSpace {
                margin-top: 15%;
            }
            #mods {
                background-color: #3AC578;
                padding-top: 1.5%;
                padding-bottom: 1.5%;
                box-shadow: 0px 5px 5px #888888;
                border-radius: 10px;
            }
        </style>
    </head>

    <body>
        <div class="container-fluid">
            <div class="row topSpace" id="content">
                <div class="col-xs-4 col-md-4"></div>

                <div class="col-xs-3 cod-md-3" id="mods">
                    <form class="form-horizontal" role="form">
                        <div class="form-group">
                            <div class="col-sm-12">
                                <input type="text" class="form-control" id="qlid" placeholder="Quicklook ID" maxlength="8">
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-12">
                                <input type="password" class="form-control" id="pwd" placeholder="Password" maxlength="16">
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-offset-1 form-inline">
                                <div class="checkbox">
                                    <label>
                                        <input id="chkbx" type="checkbox"> Remember me
                                    </label>
                                </div>

                                <button type="submit" class="btn btn-primary col-sm-offset-4" id="submitBtn">Sign in</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </body>
</html>
英文:

I'm learning how to make a backend with Go and I made a simple server that just loads a login page. When the page is loaded the external css and js files are not being loaded. When I looked to see what was wrong in the console I got this message "Resource interpreted as Stylesheet but transferred with MIME type text/html: "http://localhost:8081/login/css/bootstrap.min.css". " I get this error for all the css and js files included in the html file.

Here is the Go code:

package main
import(
&quot;net/http&quot;
)
func loginFunc(w http.ResponseWriter, r *http.Request){
http.ServeFile(w, r, &quot;index.html&quot;)
}
func main(){
http.HandleFunc(&quot;/login/&quot;, loginFunc);
http.ListenAndServe(&quot;:8081&quot;, nil);
}

Here's the html:

    &lt;!DOCTYPE HTML&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;
&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;css/bootstrap.min.css&quot;&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;js/jquery-2.1.1.min.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;js/bootstrap.min.js&quot;&gt;&lt;/script&gt;
&lt;style type=&quot;text/css&quot;&gt;
.topSpace{
margin-top: 15%;
}
#mods{
background-color: #3AC578;
padding-top: 1.5%;
padding-bottom: 1.5%;
box-shadow: 0px 5px 5px #888888;
border-radius: 10px;
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div class=&quot;container-fluid&quot;&gt;
&lt;div class=&quot;row topSpace&quot; id=&quot;content&quot;&gt;
&lt;div class=&quot;col-xs-4 col-md-4&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;col-xs-3 cod-md-3&quot; id=&quot;mods&quot;&gt;
&lt;form class=&quot;form-horizontal&quot; role=&quot;form&quot;&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;div class=&quot;col-sm-12&quot;&gt;
&lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;qlid&quot;  placeholder=&quot;Quicklook ID&quot; maxlength=&quot;8&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;div class=&quot;col-sm-12&quot;&gt;
&lt;input type=&quot;password&quot; class=&quot;form-control&quot; id=&quot;pwd&quot;  placeholder=&quot;Password&quot; maxlength=&quot;16&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;div class=&quot; col-sm-offset-1 form-inline&quot;&gt;
&lt;div class=&quot;checkbox&quot;&gt;
&lt;label&gt;
&lt;input id=&quot;chkbx&quot; type=&quot;checkbox&quot;&gt; Remember me
&lt;/label&gt;
&lt;/div&gt;
&lt;button type=&quot;submit&quot; class=&quot;btn btn-primary col-sm-offset-4&quot; id=&quot;submitBtn&quot;&gt;Sign in&lt;/button&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

答案1

得分: 2

一种方法是使用nginx,它非常适用于这种任务,并且是使用https的推荐方式。

一个示例配置如下:

server {
listen       0.0.0.0:80;
charset utf-8;
location /s/  {
alias /path/to/app/static/;
autoindex off;
}
location / {
proxy_set_header        Host             $host;
proxy_set_header        X-Real-IP        $remote_addr;
proxy_set_header        X-Forwarded-For  $proxy_add_x_forwarded_for;
# where 9020 is the port your go app is running on
proxy_pass http://127.0.0.1:9020; 
proxy_redirect off;
}
location = /favicon.ico { access_log off; log_not_found off; }
}

参考资料:

英文:

One way is to use nginx, it's extremely optimized for that kind of tasks, and it is the recommended way to use https.

An example conf is:

server {
listen       0.0.0.0:80;
charset utf-8;
location /s/  {
alias /path/to/app/static/;
autoindex off;
}
location / {
proxy_set_header        Host             $host;
proxy_set_header        X-Real-IP        $remote_addr;
proxy_set_header        X-Forwarded-For  $proxy_add_x_forwarded_for;
# where 9020 is the port your go app is running on
proxy_pass http://127.0.0.1:9020; 
proxy_redirect off;
}
location = /favicon.ico { access_log off; log_not_found off; }
}

references:

huangapple
  • 本文由 发表于 2014年9月30日 10:27:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/26111815.html
匿名

发表评论

匿名网友

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

确定