Skip to content

Instantly share code, notes, and snippets.

@kidandcat
Created April 15, 2019 14:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kidandcat/61265885cb00225722bf2c5b3e69b694 to your computer and use it in GitHub Desktop.
Save kidandcat/61265885cb00225722bf2c5b3e69b694 to your computer and use it in GitHub Desktop.
Server in Nim
import asynchttpserver, asyncdispatch, json
var server = newAsyncHttpServer()
proc handler(req: Request) {.async.} =
case req.url.path:
of "/hello-world":
let msg = %* {"message": "Hello World"}
let headers = newHttpHeaders([
("Content-Type","application/json"),
("Access-Control-Allow-Origin","*")
])
await req.respond(Http200, $msg, headers)
else:
await req.respond(Http404, "Not Found")
echo "Listening on port 4000"
waitFor server.serve(Port(4000), handler)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment