Luminance
Luminance

Reputation: 918

Golang http.FileServer returns 404 not found

Having this simple snippet:

    fs := http.FileServer(http.Dir("./web/js"))
    http.Handle("/js/", http.StripPrefix("/js/", fs))

and going to /js/ actually lists the files, but when I try to open the actual file it says 404 Not Found

$ curl http://localhost:8100/js/
<pre>
<a href="test.js">test.js</a>
</pre>

$ curl http://localhost:8100/js/test.js
404 page not found

Any suggestions? It seems like a super trivial issue.

Upvotes: 0

Views: 85

Answers (1)

Luminance
Luminance

Reputation: 918

The problem was not in the code snippet, but rather obscuring details like using gorilla/mux which serves files differently as pointed in this solution:

Solution

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))

Upvotes: 3

Related Questions