Starting a small local web server with HTTP.jl?

Oops, my bad. It should be

HTTP.listen() do req::HTTP.Request
    req.target == "/" && return HTTP.Response(200, read("index.html"))
    file = HTTP.unescapeuri(req.target[2:end]))
    return isfile(file) ? HTTP.Response(200, read(file)) : HTTP.Response(404)
end

req.target == "/" && return HTTP.Response(200, read("index.html")) is a default handler where you navigate to 127.0.0.1:8081, typically in that case, servers return the “default” webpage, usually named “index.html”.
file = HTTP.unescapeuri(req.target[2:end])), takes any path and converts it into a file, so if you visited 127.0.0.1:8081/path/to/file it would return path/to/file; the call to unescapeuri just ensures that any % encoded characters are converted back from the url encoding.

The last line just checks if the path refers to an actual file: if so, it reads and returns it, otherwise, a 404 response is returned.

2 Likes