Serve Static Assets (using the Mux Router)
·
265 words
·
2 minutes read
Using a router is great when passing off incoming requests to functions to handle and return data. Often though, you just want to serve an entire directory and make everything inside it public. This is useful for images, styles and javascript.
In this example we’re using the Gorilla mux router (“HTTP request multiplexer”) and we have setup a new route for the entire directory. We’re using static
as the folder name to serve, which we pass to our FileServer()
as a new route on the router.
|
|
Without gorilla/mux
Although this post is tailored towards when you are using the gorilla mux router, it’s also good to know how to do it the default stdlib way (it’s actually very similar). The code below should result in the same out come as the example above.
|
|