Skip to content

Instantly share code, notes, and snippets.

@btbytes
Created August 6, 2024 15:30
Show Gist options
  • Save btbytes/9a9f45047ce8ec38c9d31dba126f7b26 to your computer and use it in GitHub Desktop.
Save btbytes/9a9f45047ce8ec38c9d31dba126f7b26 to your computer and use it in GitHub Desktop.
static file server in nim
import std/[asynchttpserver, asyncdispatch, os, strutils]
proc handleRequest(req: Request) {.async.} =
let path = "." & req.url.path
if fileExists(path):
let content = readFile(path)
let contentType = case path.splitFile.ext
of ".html": "text/html"
of ".css": "text/css"
of ".js": "application/javascript"
else: "application/octet-stream"
await req.respond(Http200, content, newHttpHeaders({"Content-Type": contentType}))
else:
await req.respond(Http404, "404 Not Found")
proc main() =
let server = newAsyncHttpServer()
waitFor server.serve(Port(8080), handleRequest)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment