This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Michael Nikitochkin
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)'
References
This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Michael Nikitochkin
Michael Nikitochkin | Sciencx (2023-02-19T17:30:00+00:00) Minimalist web server for static files with Crystal. Retrieved from https://www.scien.cx/2023/02/19/minimalist-web-server-for-static-files-with-crystal/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.