Skip to content

Commit 45c8e66

Browse files
committed
Improve Go server
- log to stdout when starting the server - send updated data on POST - set content type headers
1 parent 18725dd commit 45c8e66

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

server.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ func handleComments(w http.ResponseWriter, r *http.Request) {
6868
return
6969
}
7070

71+
w.Header().Set("Content-Type", "application/json")
72+
io.Copy(w, bytes.NewReader(commentData))
73+
7174
case "GET":
75+
w.Header().Set("Content-Type", "application/json")
7276
// stream the contents of the file to the response
7377
io.Copy(w, bytes.NewReader(commentData))
7478

@@ -81,5 +85,6 @@ func handleComments(w http.ResponseWriter, r *http.Request) {
8185
func main() {
8286
http.HandleFunc("/comments.json", handleComments)
8387
http.Handle("/", http.FileServer(http.Dir("./public")))
88+
log.Println("Server started: http://localhost:3000")
8489
log.Fatal(http.ListenAndServe(":3000", nil))
8590
}

0 commit comments

Comments
 (0)