Allan
Allan

Reputation: 1

Beego-router:When I use multiple level route, cannot find the correct static file

  1. go version go1.14 darwin/amd64
  2. beego 1.12.1
  3. When my url is just one level, everything is ok

beego.Router("/index", &controllers.HomeController{}, "get:Index")

  1. But when my url is more than one level, the program cannot find static file.

beego.Router("/summary/:all",&controllers.SummaryVersionController{})

Here is controller code :

pathurl := this.Ctx.Input.Param(":all")
var s string = "'"
pathurl = fmt.Sprintf("%s%s%s",s,pathurl,s)
fmt.Println(pathurl)
table,err:= new(models.JobList).SummaryForVersionData(pathurl)
if err!=nil{
    logs.Error("SummaryVersionController => ", err)
    this.Abort("404")
}
this.Data["Contents"]=table
this.TplName = "600.html"

Here is the console output when i try to tyoe URL "http://127.0.0.1:8080/summary/6.0.0"

2020/07/11 19:22:23.554 [D] [server.go:2807]  |      127.0.0.1| 200 |   5.849898ms|   match| GET      /summary/6.0.0   r:/summary/:all
2020/07/11 19:22:23.581 [D] [server.go:2807]  |      127.0.0.1| 404 |    215.983µs| nomatch| GET      /summary/static/assets/css/app.min.css
2020/07/11 19:22:23.590 [D] [server.go:2807]  |      127.0.0.1| 404 |    460.945µs| nomatch| GET      /summary/static/assets/bundles/datatables/datatables.min.css
2020/07/11 19:22:23.590 [D] [server.go:2807]  |      127.0.0.1| 404 |    256.697µs| nomatch| GET      /summary/static/assets/bundles/datatables/DataTables-1.10.16/css/dataTables.bootstrap4.min.css
2020/07/11 19:22:23.593 [D] [server.go:2807]  |      127.0.0.1| 404 |    650.812µs| nomatch| GET      /summary/static/assets/css/style.css

Actually , the program should find static file in /static, not /summary/static, not sure why add the route string summary.

Upvotes: 0

Views: 334

Answers (1)

ahmetlutfu
ahmetlutfu

Reputation: 325

You should use base tag in html head.

  <base href="http://yourhost/">

or you should include assets with absolute path.

<link rel="stylesheet" href="/static/style.css">

if you don't set the base url, the browser sends request for relative path.

Also do not forget to set static path.

beego.SetStaticPath("/static","static")

Upvotes: 0

Related Questions