What I tried

I am fairly new to golang, and front-end programming in general. I have the following file structure.

HTML folder containing Landing_Page Folder and ADD_Task folder. Landing_Page folder contains index.html and style.css. Similarly, ADD_Task also contains its respective index.html and style.css. My go source code is found at the same level as the HTML folder (not in it)

So each of these folders, both have their respective index and style files. I would like my web-server to serve both the index and stylesheet under the landing_page folder when I enter / in the url. Similarly, /add would serve the html and css in the ADD_Task folder (a sub directory of HTML - just like Landing_Page).

Now I am trying to do the following using the net/http library.

To do this my attempt goes as follows, I create a ServeMux, with respective mux.HandleFunc for urls containing / and /add.

i.e:

mux := http.NewServeMux()
mux.HandleFunc("/", homeHandler)
mux.HandleFunc("/add", addHandler)

Then for each respective handler, I set the appropriate path, at the beginning of the call. For the home handler I perform http.FileServer(http.Dir("./HTML/Landing_Page")).ServeHTTP(w,r).

Whereas, for the add handler

r.URL.Path = strings.TrimPrefix(r.URL.Path, "/add") #removing this, since it is added with /add 
http.FileServer(http.Dir("./HTML/ADD_Task")).ServeHTTP(w,r)

The issue

What I fail to understand, is that the / url works perfectly, whereas the /add reads the html but the not css.

I went through all the obvious stuff, such as name conflicts, ensured the file is under the same directory etc. From debugging I understand that it is unable to find this css file, most likely due to some directory issue, returning a 404 error (under the network tab the following request returns the 404 - http://localhost:8080/style.css). From what I can understand, I somehow need to provide the path for my css like I did to html, however, the html should link to the css with the link tag, as was the case with the / url. In the appropriate html file, I have the following: <link rel="stylesheet" type="text/css" href="style.css"/> within the head tag.

Hope this was clear, and any help would be greatly appreciated, as I've been unable to solve this for a few weeks now.

0

There are 0 best solutions below