Written by
Michael Nikitochkin
on
on
Minimalist web server for static files with Crystal
Minimalist web server for static files with Crystal
Problem
Build a cross-platform web server to serve static files in the local folder. Test local HTML, CSS, and JS files without CORS errors1.
Many browsers, including Firefox and Chrome, now treat all local files as having opaque origins (by default). As a result, loading a local file with included local resources will result in CORS errors.
Solutions
The standard Crystal library allows an HTTP server to process requests2.
Standard
Run a server on http://127.0.0.1:8080
to serve files from the local directory.
# server.cr
require "http/server"
def run(host="127.0.0.1", port=8080, local=".")
server = HTTP::Server.new([
HTTP::ErrorHandler.new,
HTTP::LogHandler.new,
HTTP::CompressHandler.new,
HTTP::StaticFileHandler.new(local),
])
address = server.bind_tcp host, port
puts "Listening on http://#{address}"
server.listen
end
run
$ crystal run server.cr
Listening on http://127.0.0.1:8080
Minimalist
Of course, you want to run it everywhere without creating additional files:
$ crystal eval -p 'require "http/server"; HTTP::Server.new([HTTP::StaticFileHandler.new(".")]).listen(8080)'