【问题标题】:MIme types not correct when using Go server使用 Go 服务器时 MIme 类型不正确
【发布时间】:2014-09-30 11:13:13
【问题描述】:

我正在学习如何使用 Go 制作后端,并且我制作了一个仅加载登录页面的简单服务器。加载页面时,未加载外部 css 和 js 文件。当我查看控制台中有什么问题时,我收到此消息“资源解释为样式表,但使用 MIME 类型 text/html 传输:“http://localhost:8081/login/css/bootstrap.min.css”。“我收到包含在html文件。

这是 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>

                    </form>
                </div>
            </div>
        </div>
    </body>
</html>

【问题讨论】:

  • 嗯,您设置的代码根本不包括提供静态文件目录,例如 /css/js。要从 Go 中执行此操作,this answer 建议使用 http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("./css/"))))。将 nginx 放在 Go 服务器前面也很常见,以处理静态文件服务等任务。
  • 如何将 nginx 放在我的 Go 服务器前面?我是http游戏新手。
  • 除非您需要提供自定义资源,否则您也可以只为 Bootstrap 和 JQuery 使用现有的 CDN 之一。 bootstrapcdn.comcode.jquery.com

标签: html css http go


【解决方案1】:

一种方法是使用 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; }
}

参考:

【讨论】:

    猜你喜欢
    • 2018-05-15
    • 2020-09-18
    • 1970-01-01
    • 2011-07-28
    • 2021-04-24
    • 2011-08-20
    • 2012-11-29
    • 2012-04-13
    相关资源
    最近更新 更多