html - 使用 Go 服务器时 MIme 类型不正确

标签 html css http go

我正在学习如何使用 Go 制作后端,并且制作了一个仅加载登录页面的简单服务器。加载页面时,不会加载外部 css 和 js 文件。当我查看控制台中的问题时,我收到此消息“资源被解释为样式表,但使用 MIME 类型文本/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>

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

最佳答案

一种方法是使用 nginx,它针对这类任务进行了极度优化,这是使用 https 的推荐方法。

一个示例 conf 是:

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; }
}

引用资料:

关于html - 使用 Go 服务器时 MIme 类型不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26111815/

相关文章:

html - 如何将一张图片放在另一张图片之上?

html - 如何通过 CSS 删除最后一个 div 分隔符的 border-bottom?

apache - 是否可以将端口 80 用于 HTTP 和 Web 套接字流量?

http - 如何保护公网IP设备的web登录

php - 检查 Dropbox 应用程序文件夹是否存在

html - 错误的 URI Ruby on Rails : is it because long?

html - 为 mat-select 预选多个值 - Angular 6

html - ThreejS : Uncaught TypeError: e. OBJMTLLoader 不是构造函数

php - Bootstrap 列在 Wordpress 上发生冲突

javascript - jQuery UI 可对整个 div 进行排序