On Mon, May 6, 2019 at 9:47 AM Richard Tenney <rlten...@gmail.com> wrote: > > Hi. I remain mystified. I tried your suggestion, but it didn't work. > > This is .../learn/main.go > func main() { > > http.HandleFunc("/", handleRoot) > http.Handle("/css/", http.FileServer(http.Dir("webpages/css"))) > http.Handle("/js/", http.FileServer(http.Dir("webpages/js"))) > > fmt.Println("listening on port 8877") > http.ListenAndServe(":8877", nil) > }
You need to set the content type on the response. Try something like this: func rewriteContentType(contentType string,handler http.Handler) http.Handler { return http.HandlerFunc( func(wr http.ResponseWriter,req *http.Request) { wr.Header().Set("Content-Type",contentType) handler.ServeHTTP(wr,req) }) } and then: http.Handle("/css/", rewriteContentType("text/css".http.FileServer(http.Dir("webpages/css")))) http.Handle("/js/", rewriteContentType("text/javascript".http.FileServer(http.Dir("webpages/js")))) This code probably will not compile, but it should give you the idea. > > > The files aren't found -- as soon as http://127.0.0.1:8877/ is loaded the > two error messages produced are: > > Refused to apply style from 'http://127.0.0.1:8877/css/main.css' because its > MIME type ('text/plain') is not a supported stylesheet MIME type, and strict > MIME checking is enabled. > 127.0.0.1/:6 GET http://127.0.0.1:8877/js/file.js net::ERR_ABORTED 404 (Not > Found) > > > But simply displaying .../learn/webpages/main.html via chrome works just fine. > > For the record, .../learn/webpages/main.html: > <!DOCTYPE HTML> > <html> > <head> > <meta charset="utf-8"> > <link rel="stylesheet" href="css/main.css"> > <script type="text/javascript" src="js/file.js" ></script> > </head> > <body> > <div> > <h2> file location exercise </h2> > <input type="button" onclick="f1()" value="Find File" /> > </div> > </body> > </html> > > .../learn/webpages/css/main.css: > body { > background-color: Azure; > color: blue > } > > and .../learn/webpages/js/file.js: > function f1() { > alert("Hello from .../learn/webpages/js/file.js") > } > > So, I'm still in need of help. > > Thanks! > > On Mon, May 6, 2019 at 1:02 AM Burak Serdar <bser...@ieee.org> wrote: >> >> 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. -- 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.