Serving static files over HTTP
While designing web applications, it’s always a best practice to serve static resources, such as .js
, .css
, and images
from the filesystem, or any content delivery network (CDN), such as Akamai or Amazon CloudFront, rather than serving it from the web server. This is because all these types of files are static and do not need to be processed; so why should we put extra load on the server? Moreover, it helps to boost application performance, as all the requests for the static files will be served from external sources and therefore reduce the load on the server.
Go's net/http
package is sufficient enough for serving static resources from the filesystem through FileServer
, which we will be covering in this recipe.
Getting ready…
As we have already created a template in our previous recipe, we will just extend it to serve a static .css
file from the static/css
directory.
How to do it…
In this recipe, we are going to create a file server that will serve static resources...