hertz: [QUESTION] - Static function not working properly
I’m trying to serve css/js and other assets using Static(relativePath, root string)
in html file but it doesn’t seem to work properly.
I’ve this code
package main
import (
"context"
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/app/server"
"github.com/cloudwego/hertz/pkg/common/utils"
)
func main() {
h := server.Default(server.WithHostPorts("127.0.0.1:8080"))
h.LoadHTMLGlob("./resources/views/layouts/*")
h.Static("/file", "./public")
h.GET("/html", func(c context.Context, ctx *app.RequestContext) {
ctx.HTML(200, "master.html", utils.H{})
})
h.Spin()
}
In example directory I’ve this setup
Since I used prefix /file
, I tried to use <link rel="stylesheet" href="/file/css/style.css">
in master.html as in
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="/file/css/style.css">
<title>{{.title}}</title>
</head>
<body>
Master Layout
</body>
</html>
But the css is not loading resulting 404 on browser and following error terminal
2022/11/20 18:05:52.503943 fs.go:858: [Error] HERTZ: Cannot open file="./public/file", error=open ./public/file: no such file or directory
2022/11/20 18:05:59.110850 fs.go:858: [Error] HERTZ: Cannot open file="./public/file/css/style.css", error=open ./public/file/css/style.css: no such file or directory
Please suggest if I’m doing anything wrong
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 18 (10 by maintainers)
PR created
@li-jin-gou Sure let me do it tomorrow morning. Do you think we should add third param in
Static()
as config similar to the way I did?