Working with static files

I created a static folder that contains index.html file, and in my go file, I wrote:

package main

import (
	"net/http"
)

func main() {
	http.Handle("/", http.FileServer(http.Dir("./static")))

	http.ListenAndServe(":8482", nil)
}

And it works fine upon exploring http://localhost:8482/

I tried to write the code as:

http.Handle("/static", http.FileServer(http.Dir("./static")))

But it fails upon exploring http://localhost:8482/static with 404 error

There is an example in the standard library but seems to be wrong. The following code works.

http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.