最近的一個全棧項目,光伏雲監控系統,后端使用beego框架,純api,前端使用VUE2.0。項目地址:http://scada.ssechina.com:88/static
我把打包好的前端文件放到go的static目錄,
然后main里面設置
beego.BConfig.WebConfig.StaticDir["/static"] = "static"
只能用ip/static/login.html來訪問
如果改成
beego.BConfig.WebConfig.StaticDir["/"] = "static"
就訪問不了了
但是網址中多了個static怪怪的,而且確實有很多文件比如驗證網站歸屬,需要在根目錄放一個靜態文件這種需求,希望直接以根目錄訪問靜態文件
Beego怎樣用根目錄來訪問靜態文件?在網上找到以下辦法感覺不錯,留待下個項目使用:
在main.go里增加了下面的代碼
//透明static beego.InsertFilter("/", beego.BeforeRouter, TransparentStatic) beego.InsertFilter("/*", beego.BeforeRouter, TransparentStatic) func TransparentStatic(ctx *context.Context) { if strings.Index(ctx.Request.URL.Path, "v1/") >= 0 { return } http.ServeFile(ctx.ResponseWriter, ctx.Request, "static/"+ctx.Request.URL.Path) }