bashgaa
(bashgaa)
June 25, 2018, 4:11am
1
i am integrating hero template to fasthttp but localhost:8080 shows just like this
but i need localhost:8080 shows just like this
-Alice
-Bob
-Tom
problem is html code is shown. My app.go is
package main
import (
"bytes"
"log"
"github.com/shiyanhui/hero/examples/app/template"
"github.com/valyala/fasthttp"
)
func main() {
requestHandler := func(ctx *fasthttp.RequestCtx) {
var userList = []string{
"Alice",
"Bob",
"Tom",
}
buffer := new(bytes.Buffer)
template.UserList(userList, buffer)
if _, err := ctx.Write(buffer.Bytes()); err != nil {
log.Printf("ERR: %s\n", err)
}
}
log.Fatal(fasthttp.ListenAndServe(":8080", requestHandler))
}
NobbZ
(Norbert Melzer)
June 25, 2018, 6:05am
2
Just a guess as I do not know fasthttp, but I think you need to set an appropriate content type.
2 Likes
NobbZ
(Norbert Melzer)
June 25, 2018, 10:08am
3
PS: You are generating three lists with a single item each, is this really the expected behaviour?
robertwe
(Robert W.)
June 26, 2018, 6:51pm
4
Hi,
the browser needs an info what type of content you will serve.
In you case before you set the buffer you need to set the proper header:
ctx.SetContentType("text/html; charset=utf8")
After that it should work.
robertwe
(Robert W.)
June 26, 2018, 6:52pm
5
here you can find more complex example:
package main
import (
"flag"
"fmt"
"log"
"github.com/valyala/fasthttp"
)
var (
addr = flag.String("addr", ":8080", "TCP address to listen to")
compress = flag.Bool("compress", false, "Whether to enable transparent response compression")
)
func main() {
flag.Parse()
h := requestHandler
if *compress {
This file has been truncated. show original
system
(system)
Closed
September 25, 2018, 1:56pm
7
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.