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 …


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

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

  2. Crystal: class HTTP::Server ↩


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Michael Nikitochkin


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » Minimalist web server for static files with Crystal." Michael Nikitochkin | Sciencx - Sunday February 19, 2023, https://www.scien.cx/2023/02/19/minimalist-web-server-for-static-files-with-crystal/
HARVARD
Michael Nikitochkin | Sciencx Sunday February 19, 2023 » Minimalist web server for static files with Crystal., viewed ,<https://www.scien.cx/2023/02/19/minimalist-web-server-for-static-files-with-crystal/>
VANCOUVER
Michael Nikitochkin | Sciencx - » Minimalist web server for static files with Crystal. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/02/19/minimalist-web-server-for-static-files-with-crystal/
CHICAGO
" » Minimalist web server for static files with Crystal." Michael Nikitochkin | Sciencx - Accessed . https://www.scien.cx/2023/02/19/minimalist-web-server-for-static-files-with-crystal/
IEEE
" » Minimalist web server for static files with Crystal." Michael Nikitochkin | Sciencx [Online]. Available: https://www.scien.cx/2023/02/19/minimalist-web-server-for-static-files-with-crystal/. [Accessed: ]
rf:citation
» Minimalist web server for static files with Crystal | Michael Nikitochkin | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.