Html 使用Go服务器时MIme类型不正确
我正在学习如何使用Go制作后端,我制作了一个简单的服务器,只需加载一个登录页面。加载页面时,不会加载外部css和js文件。当我查看控制台中的错误时,我收到了这样一条消息:“资源被解释为样式表,但使用MIME类型text/html传输:”Html 使用Go服务器时MIme类型不正确,html,css,http,go,Html,Css,Http,Go,我正在学习如何使用Go制作后端,我制作了一个简单的服务器,只需加载一个登录页面。加载页面时,不会加载外部css和js文件。当我查看控制台中的错误时,我收到了这样一条消息:“资源被解释为样式表,但使用MIME类型text/html传输:”http://localhost:8081/login/css/bootstrap.min.css“”对于html文件中包含的所有css和js文件,我都会遇到此错误 以下是Go代码: package main import( "net/http"
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>
.顶部空间{
利润率最高:15%;
}
#mods{
背景色:#3AC578;
垫面:1.5%;
垫底:1.5%;
盒影:0px 5px 5px#8888888;
边界半径:10px;
}
记得我吗
登录
一种方法是使用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; }
}
参考资料:
- 个人经历
/css
或/js
。要从Go开始执行,建议使用http.Handle(“/css/”、http.StripPrefix(“/css/”、http.FileServer(http.Dir(“/css/”))。将nginx放在Go服务器前面以处理静态文件服务之类的任务也是很常见的。如何将nginx放在Go服务器前面?我是http游戏的新手。你也可以使用一个已经可用的CDN进行引导和JQuery,除非你需要提供自定义资源。