On Sat, May 4, 2019 at 10:20 PM ThisEndUp <rlten...@gmail.com> wrote: > > I am thoroughly frustrated. I have a file named main.html that references a > css and a js file. > > Here's the relevant portion. > <head> > <meta charset="utf-8"> > <link rel="stylesheet" href="css/main.css"> > <script type="text/javascript" src="js/file.js" ></script> > </head> > > This is in a subdirectory of my GOPATH/src, named learn/webpages, and the css > and js files are in subdirectories of that, i.e., learn/webpages/css/main.css > and learn/webpages/js/file.js > > When I open the file main.html in my Chrome browser, it behaves just as I'd > expect it to. > > When I try to serve it up via a go program, main.go in the learn directory > (i.e., learn/main.go) I can't for the life of me figure out what magic > configuration of http.HandleFunc, http.FileServer, http.Handle, > http.StripPrefix, and http.Dir to use to get these files to load. I don't > want to use absolute paths, since the location of the base directory will > change over time. > > Here's the directory structure of the project. I've put a version of file.js > and main.css into three different places (with slightly different contents, > so I can tell which one loads, if any does). The ones I want are in the > appropriate subdirectories of the webpages directory. > > [learn] > [js] > file.js > [webpages] > [css] > main.css > [js] > file.js > main.css > main.html > file.js > main.css > main.exe > main.go > > Key: [xxx] is a directory and its contents are indented below it. > > The following does not work, but it's the direction I've been trying. The > func handleRoot loads the web page, but I can't figure out how to access the > css and js files. And when the documentation refers to the "current > directory" is that going to be learn, where main.exe is running, or > learn/webpages where main.html lives? > > > // main.go > package main > > import ( > "fmt" > "net/http" > ) > > func handleRoot(w http.ResponseWriter, r *http.Request) { > http.ServeFile(w, r, "webpages/main.html") > } > > func main() { > http.HandleFunc("/", handleRoot) ^^^^^^ This will handle requests for "/" to handleRoot, which is correct
> > fs := http.FileServer(http.Dir("webpages")) > http.Handle("/webpages/", http.StripPrefix("/webpages/", fs)) ^^^^^^^^^^^^^^^^^^^^^^^^ This will handle requests for /webpages/ to fs, stripping the prefix. However, with the HTML file you have, the requests will be /css and /js, not /webpages/css or /webpages/js You could try this: http.Handle("/css/",http.FileServer(http.Dir("webpages/css")) http.Handle("/js/",http.FileServer(http.Dir("webpages/js")) > > fmt.Println("listening on port 8899") > http.ListenAndServe(":8899", nil) > } > > > I've looked at lots of webpages that purport to explain how to do this, but > haven't yet figured any of them out. Any help will be greatly appreciated. > > > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.