Minimalist web server for static files with Crystal

Michael Nikitochkin - Feb 19 '23 - - Dev Community

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
Enter fullscreen mode Exit fullscreen mode
$ crystal run server.cr
Listening on http://127.0.0.1:8080
Enter fullscreen mode Exit fullscreen mode

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)'
Enter fullscreen mode Exit fullscreen mode

References


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

  2. Crystal: class HTTP::Server 

. . . . . . . . . . . . .