In this section, we're going to see how to create a simple file server. The main idea behind a file server is to serve static files, such as images, or CSS files, or JavaScript files, and, in our code, we're going to see how to do this. Check the following code:
package main import "net/http" func main() { http.Handle("/", http.FileServer(http.Dir("./images"))) http.ListenAndServe(":5050", nil) }
As you can see, we have used the HTTP handle, and this Handle
is different from handleFunc
and accepts a handler interface as a second parameter; the first parameter is pattern
. We are going to use a special API called FileServer
and it will work as a file server here; we will add a location (image directory, ./images
) in the server to serve the static files.
So, what's going to happen is that when the request hits the route path, a file server will serve the requests and it will give static files under the location http.Dir("./images"
). We are going to use http...