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)'

References

  1. Reason: CORS request not HTTP: Loading a local file ↩

  2. Crystal: class HTTP::Server ↩

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
challenge-accepted

Challenge Accepted

Next Post
cli-client-for-reductstore-v07.0-has-been-released

CLI Client for ReductStore v0.7.0 has been released

Related Posts