main.go
package main
import (
"net/http"
)
func main() {
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
http.ListenAndServe(":8080", nil)
}
Directory structure:
%GOPATH%/src/project_name/main.go
%GOPATH%/src/project_name/static/..files and folders ..
Even after reading the documentation I have trouble understanding what exactly http.StripPrefix
does here.
1) Why can't I access localhost:8080/static
if I remove http.StripPrefix
?
2) What URL maps to /static
folder if I remove that function?
http.StripPrefix()
forwards the handling of the request to one you specify as its parameter, but before that it modifies the request URL by stripping off the specified prefix.So for example in your case if the browser (or an HTTP client) requests the resource:
StripPrefix
will cut the/static/
and forward the modified request to the handler returned byhttp.FileServer()
so it will see that the requested resource isThe
Handler
returned byhttp.FileServer()
will look for and serve the content of a file relative to the folder (or ratherFileSystem
) specified as its parameter (you specified"static"
to be the root of static files).Now since
"example.txt"
is in thestatic
folder, you have to specify a relative path to that to get the corrent file path.Another Example
This example can be found on the http package documentation page (here):
Explanation:
FileServer()
is told the root of static files is"/tmp"
. We want the URL to start with"/tmpfiles/"
. So if someone requests"/tempfiles/example.txt"
, we want the server to send the file"/tmp/example.txt"
.In order to achieve this, we have to strip
"/tmpfiles"
from the URL, and the remaining will be the relative path compared to the root folder"/tmp"
which if we join gives:Assume that
I have a file
Then, tell fileserve serves local directory
Example 1 - root url to Filerserver root
Now file server takes
"/"
request as"/home/go/src/js"+"/"
Yes,
http://localhost/kor.js
request tells Fileserver, findkor.js
inwe got
kor.js
file.Example2 - custom url to Fileserver root
But, if we add additional resquest name.
Now file server takes
"/static/"
request as"/home/go/src/js"+"/static/"
Yes,
http://localhost/static/kor.js
request tells Fileserver, findkor.js
inWe got 404 error.
Example 3 - custom url to Fileserver root with
so, we modify request url before Fileserver takes it with
http.StripPrefix("/tmpfiles/", ...
after
stripPrefix
Fileserver take/
instead/static/
got
kor.js
I'll answer the two questions one by one.
Answer 1 to Question 1: If your code is written like below:
your root folder is
%GOPATH%/src/project_name/static/
. When you accesslocalhost:8080/static
, the URL/static
will be forwarded to the handler returned by http.FileServer(). However, you have no directory or file namedstatic
in the root folder.Answer 2 to Question 2: In general, you cannot access
/static
folder if you remove the http.StripPrefix(). However, if you have a directory or file namedstatic
, you can access it (exactly not the root directory you want) with the URLlocalhost:8080:/static
.By the way, you cannot access anything if your URL does not begin with
/static
, because thehttp.ServeMux
will not redirect your request.