1

I'm trying to serve static files via http.FileServer, however it never sends back the directory I'm asking for. The code is snipped below:

func main() {

fmt.Println("Serving Files")
http.HandleFunc("/", homeFunc)
http.HandleFunc("/search", searchFunc)
http.Handle("/tmp/",
    http.StripPrefix("/tmp/", http.FileServer(http.Dir("/assets"))))

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

When visiting mywebsite.com/tmp/, text appears saying "404 page not found." A little help in case I'm missing something would be greatly appreciated!

Edit: Here's the file architecture:

main folder
|
|-/Assets
|--(assets)
|
|-main.go
daan.desmedt
  • 3,752
  • 1
  • 19
  • 33
Psaltus
  • 13
  • 1
  • 4
  • 1
    Please check [Why do I need to use http.StripPrefix to access my static files?](https://stackoverflow.com/questions/27945310/why-do-i-need-to-use-http-stripprefix-to-access-my-static-files/27946132#27946132); and [404 page not found - Go rendering css file](https://stackoverflow.com/questions/28293452/404-page-not-found-go-rendering-css-file/28294524#28294524). – icza Jun 27 '17 at 14:21
  • The go file is run from "/", attempting to find assets from the /assets file. Neither of those responses have helped me in this situation, unfortunately. – Psaltus Jun 27 '17 at 14:42

2 Answers2

2

Does the directory /assets exist? Note that /assets is an absolute path, so it must be at the root of your filesystem. If you want something in the working directory where you're executing your program, you should use ./assets.

Adrian
  • 42,911
  • 6
  • 107
  • 99
1

If you use relative path, you can check what your path is.

import (
"fmt"
"os"
)

dir, _ := os.Getwd()
fmt.Println("current path :" + dir)
Jess Chen
  • 3,136
  • 1
  • 26
  • 35