FAQ
Hey All,

I can't quite seem to get a fileserver working on a basic http server I am
running.

The directory is as follows:

/--App/
--- app.go (fileserver code)
  --public/
--- js/
-- main.js
-- etc...
Basically the App and public folders are at the same level, and since the
app.go (fileserver) is in the App folder, to access the public folder
contents I use the following code (I assume):

http.Handle("/public/", http.FileServer(http.Dir("../public")))
http.ListenAndServe(":8080", nil)

The problem is that when I go to localhost:8080/public/js/main.js, I just
get a "404 Not Found".

Since the two folders are at the same level, I can't quite figure out what
the code should be to access the contents of public?

--
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 [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Jan Mercl at Jul 26, 2013 at 8:50 am

    On Fri, Jul 26, 2013 at 10:45 AM, John-Alan Simmons wrote:
    http.Handle("/public/", http.FileServer(http.Dir("../public")))
    http.ListenAndServe(":8080", nil)
    IIRC, use: http.Handle("/public/", http.FileServer(http.Dir("")))

    -j

    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.
  • John-Alan Simmons at Jul 26, 2013 at 9:22 am
    That doesn't seem to work,
    Although I can't see why that would work, that just seems like it would
    server everything in the folder that contains app.go aka App/
    Appreciate the help though

    On Friday, July 26, 2013 4:49:39 AM UTC-4, Jan Mercl wrote:

    On Fri, Jul 26, 2013 at 10:45 AM, John-Alan Simmons
    <[email protected] <javascript:>> wrote:
    http.Handle("/public/", http.FileServer(http.Dir("../public")))
    http.ListenAndServe(":8080", nil)
    IIRC, use: http.Handle("/public/", http.FileServer(http.Dir("")))

    -j
    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.
  • Dave Cheney at Jul 26, 2013 at 9:30 am
    Which directory are you running the binary from ? If you are using Go
    run, the current working directory may be surprising.

    On Fri, Jul 26, 2013 at 7:22 PM, John-Alan Simmons
    wrote:
    That doesn't seem to work,
    Although I can't see why that would work, that just seems like it would
    server everything in the folder that contains app.go aka App/
    Appreciate the help though

    On Friday, July 26, 2013 4:49:39 AM UTC-4, Jan Mercl wrote:

    On Fri, Jul 26, 2013 at 10:45 AM, John-Alan Simmons
    wrote:
    http.Handle("/public/", http.FileServer(http.Dir("../public")))
    http.ListenAndServe(":8080", nil)
    IIRC, use: http.Handle("/public/", http.FileServer(http.Dir("")))

    -j
    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.
  • Jesse McNelis at Jul 26, 2013 at 9:30 am

    On Fri, Jul 26, 2013 at 6:45 PM, John-Alan Simmons wrote:
    http.Handle("/public/", http.FileServer(http.Dir("../public")))
    http.ListenAndServe(":8080", nil)

    The problem is that when I go to localhost:8080/public/js/main.js, I just
    get a "404 Not Found".

    Since the two folders are at the same level, I can't quite figure out what
    the code should be to access the contents of public?

    http.FileServer() looks at the url to see what file to serve. But it
    doesn't know which part is the file name and which part is the prefix from
    the handler.
    ie. when you request ''localhost:8080/public/js/main.js', http.FileServer()
    looks for the file '/public/js/main.js' in the directory you gave it.
    This ends up being '../public/public/js/main.js' which is obviously not
    what you want.

    You want.
    http.Handle("/public/",
    http.StripPrefix("/public/",http.FileServer(http.Dir("../public"))))

    As given in the http.FileServer example.
    http://golang.org/pkg/net/http/#FileServer

    --
    =====================
    http://jessta.id.au

    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.
  • Jan Mercl at Jul 26, 2013 at 9:34 am

    On Fri, Jul 26, 2013 at 11:22 AM, John-Alan Simmons wrote:
    That doesn't seem to work,
    That's strange. I've just tripple checked one of our projects - it
    really works here.

    Excerpts:

             http.Handle("/scripts/", http.FileServer(http.Dir("")))
             ...
             http.Handle("/css/", http.FileServer(http.Dir("")))
             http.Handle("/", http.HandlerFunc(home))
             log.Printf("Starting the web service @ %s\n", *oAddr)
             if err := http.ListenAndServe(*oAddr, nil); err != nil {
                     log.Fatal(err)
             }

    Elsewhere:

             <script src="scripts/codemirror/mode/javascript/javascript.js"></script>
             <script src="scripts/codemirror/mode/css/css.js"></script>
             <script src="scripts/codemirror/mode/htmlmixed/htmlmixed.js"></script>

    -j

    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.
  • John-Alan Simmons at Jul 26, 2013 at 9:56 am
    Thanks Jesse,
    That works great.
    On Friday, July 26, 2013 5:30:38 AM UTC-4, Jesse McNelis wrote:

    On Fri, Jul 26, 2013 at 6:45 PM, John-Alan Simmons <[email protected]<javascript:>
    wrote:
    http.Handle("/public/", http.FileServer(http.Dir("../public")))
    http.ListenAndServe(":8080", nil)

    The problem is that when I go to localhost:8080/public/js/main.js, I just
    get a "404 Not Found".

    Since the two folders are at the same level, I can't quite figure out
    what the code should be to access the contents of public?

    http.FileServer() looks at the url to see what file to serve. But it
    doesn't know which part is the file name and which part is the prefix from
    the handler.
    ie. when you request ''localhost:8080/public/js/main.js', http.FileServer()
    looks for the file '/public/js/main.js' in the directory you gave it.
    This ends up being '../public/public/js/main.js' which is obviously not
    what you want.

    You want.
    http.Handle("/public/",
    http.StripPrefix("/public/",http.FileServer(http.Dir("../public"))))

    As given in the http.FileServer example.
    http://golang.org/pkg/net/http/#FileServer

    --
    =====================
    http://jessta.id.au
    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJul 26, '13 at 8:45a
activeJul 26, '13 at 9:56a
posts7
users4
websitegolang.org

People

Translate

site design / logo © 2023 Grokbase