53

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?

2
  • Did you try http.Handle("/static/", http.FileServer(http.Dir("/")))?
    – Muktadir
    Dec 13, 2015 at 21:53
  • http.Handle("/static/", http.FileServer(http.Dir(""))) works.
    – Sunny
    Jul 30, 2016 at 21:54

4 Answers 4

68

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:

/static/example.txt

StripPrefix will cut the /static/ and forward the modified request to the handler returned by http.FileServer() so it will see that the requested resource is

/example.txt

The Handler returned by http.FileServer() will look for and serve the content of a file relative to the folder (or rather FileSystem) specified as its parameter (you specified "static" to be the root of static files).

Now since "example.txt" is in the static 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):

// To serve a directory on disk (/tmp) under an alternate URL
// path (/tmpfiles/), use StripPrefix to modify the request
// URL's path before the FileServer sees it:
http.Handle("/tmpfiles/",
        http.StripPrefix("/tmpfiles/", http.FileServer(http.Dir("/tmp"))))

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:

/tmp/example.txt
2
  • 8
    For anyone reading this, make sure you have the trailing slash in /tmpfiles/ or you're going to have a bad time.
    – daino3
    Sep 19, 2016 at 17:09
  • 3
    to clarify and spread the right common sense, the /tmpfile/ in Handle is important, but not in StripPrefix, let's say http.Handle("/tmpfiles/", http.StripPrefix("/tmpfiles", http.FileServer(http.Dir("/tmp")))) will also work Oct 12, 2017 at 5:58
18

Assume that

I have a file

/home/go/src/js/kor.js

Then, tell fileserve serves local directory

fs := http.FileServer(http.Dir("/home/go/src/js"))

Example 1 - root url to Filerserver root

Now file server takes "/" request as "/home/go/src/js"+"/"

http.Handle("/", fs)

Yes, http://localhost/kor.js request tells Fileserver, find kor.js in

"/home/go/src/js" +  "/"  + "kor.js".

we got kor.js file.

Example2 - custom url to Fileserver root

But, if we add additional resquest name.

http.Handle("/static/", fs)

Now file server takes "/static/" request as "/home/go/src/js"+"/static/"

Yes, http://localhost/static/kor.js request tells Fileserver, find kor.js in

"/home/go/src/js" +  "/static/"  + "kor.js".

We 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/

"/home/go/src/js" +  "/"  + "kor.js".

got kor.js

2

I'll answer the two questions one by one.

Answer 1 to Question 1: If your code is written like below:

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

your root folder is %GOPATH%/src/project_name/static/. When you access localhost:8080/static, the URL /static will be forwarded to the handler returned by http.FileServer(). However, you have no directory or file named static 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 named static, you can access it (exactly not the root directory you want) with the URL localhost:8080:/static.

By the way, you cannot access anything if your URL does not begin with /static, because the http.ServeMux will not redirect your request.

0

For anyone having issues with subdirectories, you need to add a "." because it seems it treats the path as relative only if it's a folder in the root directory of the binary.

For example

s.router.PathPrefix("/logos/").Handler(
        http.StripPrefix("/logos/", http.FileServer(http.Dir("./uploads/logos/"))))

Note the "." before "/uploads"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.