1

I hope you don't find my question stupid, but I really couldn't find an answer.

I am trying to serve my style.css file, but it won't work.

This is the structure of the project: enter image description here

In the css file i have: "link href="style.css" rel="stylesheet" type="text/css"/"

In the mainn.go I am trying to serve the css file using this: router.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("/hpages"))))

Any help will be appreciated. Thanks!

1 Answer 1

2

/hpages is an absolute path. Maybe try ./hpages instead.

The following program, run on my local machine works:

package main

import (
  "log"
  "net/http"
)

func main() {
  router := http.NewServeMux()
  router.Handle("/css/", http.StripPrefix("/css/",
                http.FileServer(http.Dir("./hpages"))))
  log.Fatal(http.ListenAndServe(":5000", router))
}
Sign up to request clarification or add additional context in comments.

4 Comments

Hey, Ive included an example that I've tried and that works. Could you check if that works on your local machine?
Hello, I've done the exact same thing, but the page shows like it would't have any css at all
Can you check your browser network tab to see if the file was received by the browser.
Yup, they weren't, found out that I was handling "/style.css" like a regular page, so of course it would not work, but now, thanks to your "./hpages" it works. Thanks a lot !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.