I am having problems with Go Fiber's Static file server. I want to load a css stylesheet which is used within a template. The template itself works fine.
func main() {
// Create a new engine.
engine := html.New("./views", ".html")
// Fiber instance
app := fiber.New(fiber.Config{Views: engine})
app.Static("/static", "./views")
app.Get("/layout", func(c *fiber.Ctx) error {
// Render index within layouts/main
return c.Render("index", fiber.Map{
"Title": "Hello, World!",
}, "layouts/main")
})
// Start server
log.Print(app.Listen(":3000"))
}
Where my file structure is: (where a -> denotes it is a directory)
-> views
-> layouts
main.css
main.html
index.html
main.go
And the contents of main.html are:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="static/layouts/main.css">
<title>Hello, world!</title>
</head>
<body>
{{embed}}
</body>
</html>
The file ./views definitely exists, as when I add this to main, nothing is printed to log:
if _, err := os.Stat("./views"); os.IsNotExist(err) {
log.Print("Path doesn't exist")
}
When I run it on localhost and use inspect element to see the network, http://127.0.0.1:3000/static/layouts/main.css is correctly queried but a 404 error is thrown. If I go to http://127.0.0.1:3000/static I get a message saying Cannot GET /static
. I think it may be a file path issue but I have tried many different combinations of .s, /s, etc and got nowhere.
How can I solve this 404 error?