So I'm taking a stab at making a simple webserver, to learn how it works, learn Nim, and of course I have great plans for the future.
But I'm having an issue where it's not returning javascript or images. It seems that the text stuff works fine (text, css), but images and javascript ends up blank.
Here's my code, it's simple and straight forward, anyone have an idea as to why it's not returning non-text properly?:
import httpserver, sockets, os, mimetypes, uri, strutils, unicode
var f: string
var mimes = newMimeTypes()
var mime: string
var pos: int
echo " "
echo " "
echo " "
echo " "
echo "Running"
proc handleRequest(client, path, query): bool =
echo path
if existsFile(path.substr(1)):
f = readFile(path.substr(1))
mime = getMimetype(mimes, path.substr(1).splitFile.ext[1 .. -1])
echo mime
echo f.len
echo ""
client.send("HTTP/1.1 HTTP/1.1 200 OK \c\L Content-Length: " & $(f.len) & " \c\L Content-Type: " & mime & " \c\L" & f)
else:
client.send("HTTP/1.1 HTTP/1.1 200 OK \c\L Content-Type: text/html \c\L 404..")
false
run(handleRequest, Port(5000))
Thanks dom96, I'll look at switching. I based it off of the rosetta code webserver hello world example.
You think that will fix the file issues..?
And indeed it did!
I simply switched, and now it all works :)