snippetgoCritical
How do you serve a static html file using a go web server?
Viewed 0 times
youhowwebusingserverhtmlfilestaticserve
Problem
How do you serve index.html (or some other static HTML file) using a go web server?
I just want a basic, static HTML file (like an article, for example) which I can serve from a go web server. The HTML should be modifiable outside of the go program, as it would be in the case while using HTML templates.
This is my web server which only hosts hard-coded text ("Hello world!").
I just want a basic, static HTML file (like an article, for example) which I can serve from a go web server. The HTML should be modifiable outside of the go program, as it would be in the case while using HTML templates.
This is my web server which only hosts hard-coded text ("Hello world!").
package main
import (
"fmt"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello world!")
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":3000", nil)
}Solution
That task is very easy with Golang net/http package.
All You need to do is:
assuming that static files are in folder named
If it's in folder
Additionally, calling any other file in that folder (for example
UPDATE: serving files from url different than "/"
If You want to serve files, say from folder
Thanks to that, all your files from
Without
This is because the server treats the whole URI as a relative path to the file.
Fortunately, it's easily solved with the built-in function.
All You need to do is:
package main
import (
"net/http"
)
func main() {
http.Handle("/", http.FileServer(http.Dir("./static")))
http.ListenAndServe(":3000", nil)
}assuming that static files are in folder named
static in the root directory of the project.If it's in folder
static, you'll have index.html file calling http://localhost:3000/ which will result in rendering that index file instead of listing all the files availible.Additionally, calling any other file in that folder (for example
http://localhost:3000/clients.html) will show that file, properly rendered by the browser (at least Chrome, Firefox and Safari :))UPDATE: serving files from url different than "/"
If You want to serve files, say from folder
./public under url: localhost:3000/static You have to use additional function: func StripPrefix(prefix string, h Handler) Handler like this:package main
import (
"net/http"
)
func main() {
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./public"))))
http.ListenAndServe(":3000", nil)
}Thanks to that, all your files from
./public are avalible under localhost:3000/staticWithout
http.StripPrefix function, if you would try to access file localhost:3000/static/test.html, the server would look for it in ./public/static/test.htmlThis is because the server treats the whole URI as a relative path to the file.
Fortunately, it's easily solved with the built-in function.
Code Snippets
package main
import (
"net/http"
)
func main() {
http.Handle("/", http.FileServer(http.Dir("./static")))
http.ListenAndServe(":3000", nil)
}package main
import (
"net/http"
)
func main() {
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./public"))))
http.ListenAndServe(":3000", nil)
}Context
Stack Overflow Q#26559557, score: 194
Revisions (0)
No revisions yet.