[question] Simple server like `five-server-vscode`
Hi there
I'm trying to create a simple localdev server similar to nimhttpd, but using mummy.
The goal is to create something similar to https://marketplace.visualstudio.com/items?itemName=yandeu.five-server
I have no experience at all creating an http server, so looking for some guidance.
- Which of the examples would be the closest to this idea?
- Can you list the tools from this library that I would need to be concerned with specifically?
To narrow it further, the goal is not to make a production-ready server. Just a local dev server that serves all files from a folder and automatically refreshes when any file changes, that's all.
Here is an idea to get started (using my own mummy-utility, you can just copy-paste the sendFile)
mummy_folder
- mummy_server.nim
- files
- image.jpg
# mummy_server.nim
import mummy, mummy/routers
import mummy_utils # https://github.com/ThomasTJdev/mummy_utils
proc serveFiles(request: Request, details: Details) =
sendFile("files/" & @"filename")
var router: Router
router.routerSet(HttpGet, "/files/images/@filename", serveFiles)
let server = newServer(router)
echo "Serving on http://localhost:8080"
server.serve(Port(8080))
curl localhost:8080/files/images/image.jpg
@ThomasTJdev tysm! How does this work in the context of serving html pages?
Managed to make the basic index.html load :partying_face:
Now the question is how to make it redirect / to /index.html :thinking:
Any tips on how to proceed?
You can make a redirect on the / to index.html route, or just check that the url == "/" and then serve the index.html.
proc redirectUno(request: Request) =
if request.uri == "/":
# request.respond(200, @[("Content-Type", newMimetypes().getMimetype(path.split(".")[^1]))], fileBody)
sendFile("index.html")
else:
echo "something else"
proc redirectDuo(request: Request) =
if request.uri == "/":
# request.respond(303, @[("Location", "index.html")])
redirect("/index.html")
else:
echo "something else"
Published the current effort on @heysokam/mummyd. That last recommendation is still pending, but the rest has worked so far :)