Home > Article > Backend Development > Golang http.FileServer returns 404 not found
php Editor Banana will introduce you to a common problem in Golang, that is, a 404 not found error is returned when using http.FileServer. When building web applications using Golang, we often use http.FileServer to serve static files. However, sometimes you will encounter a 404 not found problem when accessing static files. This article will help you solve this problem and provide some common solutions.
There is this simple fragment:
fs := http.FileServer(http.Dir("./web/js")) http.Handle("/js/", http.StripPrefix("/js/", fs))
and going to /js/
actually lists the file, but when I try to open the actual file it says 404 Not Found
$ curl http://localhost:8100/js/ <pre class="brush:php;toolbar:false"> <a href="test.js">test.js</a>$ curl http://localhost:8100/js/test.js 404 page not found
Any suggestions? This seems like a super trivial question.
The problem is not in the code snippet, but in obscuring the details, like using gorilla/mux
, which serves the files in a different way, like this solution As pointed out in:
TLDR:
import "github.com/gorilla/mux" // snip router := mux.NewRouter() fs := http.FileServer(http.Dir("./web/js")) router.Handle("/js/{.js}", http.StripPrefix("/js/", fs))
The above is the detailed content of Golang http.FileServer returns 404 not found. For more information, please follow other related articles on the PHP Chinese website!