emptygif
Ngôn ngữ: Tiếng Việt

Tất cả ngôn ngữ và framework

Nim standard library (asynchttpserver) Nim

Đoạn mã Nim cho Nim standard library (asynchttpserver), trả lời các yêu cầu tới /pixel.gif bằng GIF rỗng, được giải mã một lần khi khởi động.

Nim standard library (asynchttpserver) Nim

Tệp
server.nim
Chạy
nim c -r -d:release server.nim
import std/[asyncdispatch, asynchttpserver, base64]

# Decoded at compile time; a const is also safe to use from the gcsafe callback.
const gif = decode("R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==")

proc handler(req: Request) {.async.} =
  if req.reqMethod == HttpGet and req.url.path == "/pixel.gif":
    let headers = newHttpHeaders({"Content-Type": "image/gif", "Cache-Control": "no-store"})
    await req.respond(Http200, gif, headers)
  else:
    await req.respond(Http404, "Not Found")

let server = newAsyncHttpServer()
waitFor server.serve(Port(8080), handler)

Đã đối chiếu với tài liệu hiện hành của framework.

Kiểm tra

curl -sI http://localhost:8080/pixel.gif

Trang chính thức: Nim standard library (asynchttpserver)