diff --git a/docs/hello-state-keepalive/index.html b/docs/hello-state-keepalive/index.html index f6406fb..2dd8038 100644 --- a/docs/hello-state-keepalive/index.html +++ b/docs/hello-state-keepalive/index.html @@ -9,14 +9,18 @@ }) async function hello() { - res = await fetch('api/hello', { + const name = document.querySelector("#name").value + + const res = await fetch('api/hello', { method: 'POST', headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ name: document.querySelector("#name").value }) + body: JSON.stringify({ name }) }) + const { message } = await res.json() + alert(message) } diff --git a/docs/hello-state/api.go b/docs/hello-state/api.go index d0f3b48..a93fcd1 100644 --- a/docs/hello-state/api.go +++ b/docs/hello-state/api.go @@ -4,12 +4,13 @@ import ( "encoding/json" "fmt" "net/http" + "sync/atomic" wasmhttp "github.com/nlepage/go-wasm-http-server" ) func main() { - var no = 1 + var counter int32 http.HandleFunc("/hello", func(res http.ResponseWriter, req *http.Request) { params := make(map[string]string) @@ -19,12 +20,10 @@ func main() { res.Header().Add("Content-Type", "application/json") if err := json.NewEncoder(res).Encode(map[string]string{ - "message": fmt.Sprintf("Hello %s! (request n°%d)", params["name"], no), + "message": fmt.Sprintf("Hello %s! (request %d)", params["name"], atomic.AddInt32(&counter, 1)), }); err != nil { panic(err) } - - no++ }) wasmhttp.Serve(nil) diff --git a/docs/hello-state/api.wasm b/docs/hello-state/api.wasm index 13cb3da..c2d09c6 100755 Binary files a/docs/hello-state/api.wasm and b/docs/hello-state/api.wasm differ diff --git a/docs/hello-state/index.html b/docs/hello-state/index.html index 62fa87f..7a2fc69 100644 --- a/docs/hello-state/index.html +++ b/docs/hello-state/index.html @@ -6,14 +6,18 @@ navigator.serviceWorker.register('sw.js') async function hello() { - res = await fetch('api/hello', { + const name = document.querySelector("#name").value + + const res = await fetch('api/hello', { method: 'POST', headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ name: document.querySelector("#name").value }) + body: JSON.stringify({ name }) }) + const { message } = await res.json() + alert(message) } diff --git a/docs/hello/index.html b/docs/hello/index.html index 0303cfb..2d8e116 100644 --- a/docs/hello/index.html +++ b/docs/hello/index.html @@ -6,14 +6,18 @@ navigator.serviceWorker.register('sw.js') async function hello() { - res = await fetch('api/hello', { + const name = document.querySelector("#name").value + + const res = await fetch('api/hello', { method: 'POST', headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ name: document.querySelector("#name").value }) + body: JSON.stringify({ name }) }) + const { message } = await res.json() + alert(message) } diff --git a/response_recorder.go b/response_recorder.go index 2ae000f..f455518 100644 --- a/response_recorder.go +++ b/response_recorder.go @@ -21,6 +21,17 @@ var _ js.Wrapper = ResponseRecorder{} // JSValue builds and returns the equivalent JS Response (implementing js.Wrapper) func (rr ResponseRecorder) JSValue() js.Value { var res = rr.Result() + + var body js.Value = js.Undefined() + if res.ContentLength != 0 { + var b, err = ioutil.ReadAll(res.Body) + if err != nil { + panic(err) + } + body = js.Global().Get("Uint8Array").New(len(b)) + js.CopyBytesToJS(body, b) + } + var init = make(map[string]interface{}) if res.StatusCode != 0 { @@ -35,15 +46,5 @@ func (rr ResponseRecorder) JSValue() js.Value { init["headers"] = headers } - var body js.Value = js.Undefined() - if res.ContentLength != 0 { - var b, err = ioutil.ReadAll(res.Body) - if err != nil { - panic(err) - } - body = js.Global().Get("Uint8Array").New(len(b)) - js.CopyBytesToJS(body, b) - } - return js.Global().Get("Response").New(body, init) }