Skip to content

Instantly share code, notes, and snippets.

@haxscramper
Last active May 12, 2021 12:04
Show Gist options
  • Save haxscramper/0aef37a22c17cdf830fbe85f87de6188 to your computer and use it in GitHub Desktop.
Save haxscramper/0aef37a22c17cdf830fbe85f87de6188 to your computer and use it in GitHub Desktop.
Nim emscripten hello world

Compile nim hello world to wasm and serve it via http server.

Requirements: emcc should be available via path.

hello.nim world is just

echo "hello world"

Download nim.cfg in the same path as hello.nim, compile using nim c hello.nim, and then compile & run server using nim c -r --skipProjCfg:on httpserve.nim. Open http://127.0.0.1:8080/index.html and you should see example hello world.

import asynchttpserver, asyncdispatch, os
proc handler(req: Request) {.async.} =
echo "request for path ", req.url.path
let cwd = getCurrentDir()
let file = cwd / req.url.path
if fileExists(file):
await req.respond(Http200, file.readFile())
else:
await req.respond(Http404, "Not Found " & file)
var server = newAsyncHttpServer()
waitFor server.serve(Port(8080), handler)
d:asm
d:emscripten
o:"index.html"
cc = clang
clang.exe = "emcc"
clang.linkerexe = "emcc"
cpu = "i386"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment