【发布时间】: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