mirror of
https://github.com/nlepage/go-wasm-http-server.git
synced 2025-03-10 17:29:10 +00:00
♻️
This commit is contained in:
parent
79db7567a5
commit
04f6573b78
@ -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)
|
||||
}
|
||||
</script>
|
||||
|
@ -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)
|
||||
|
Binary file not shown.
@ -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)
|
||||
}
|
||||
</script>
|
||||
|
@ -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)
|
||||
}
|
||||
</script>
|
||||
|
@ -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)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user