Fairly new backend, specifically in Go. I'm finding it very hard to understand how backend can/should communicate to the frontend, especially in.
For example, I have a simple mock Books API in Go, which I'd like to pass the response from Go to React.
package main
import (...)
var books []Book
func main() {
// initialize router
r := mux.NewRouter()
books = append(books, []Book{
{
ID: "1",
Isbn: "23423",
Title: "Book 1",
Author: &Author{
FirstName: "Nas",
LastName: "Ahm",
},
},
{
ID: "2",
Isbn: "97878",
Title: "Book 2",
Author: &Author{
FirstName: "Has",
LastName: "Ahm",
},
},
}...)
// router endpoints
r.HandleFunc("/api/books", getBooks).Methods("GET")
log.Fatal(http.ListenAndServe(":3000", r))
}
func getBooks(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("Content-Type", "application/json")
json.NewEncoder(writer).Encode(books)
}
So the question is, how do I pass the Books data to Frontend (react)?