Rack: Serving static files using the rack-static-if-present gem

The rack-static-if-present gem is a Rack middleware service to handle static file request implicitly. If the static file doesn't exist then it would continue down the request chain for a URL match.

file: hello_world.ru

require 'rack-static-if-present'

use Rack::StaticIfPresent, :urls => ["/",'hidden.txt'], :root => "fun"

hello_world = lambda do |env|
  [200, {"Content-Type" => "text/plain"}, ["Hello World!"]]
end

run hello_world

In the above example I created a sub-directory called fun in the /tmp directory. The sub-directory contained a file called hello.txt.

To execute hello_world.ru I typed rackup hello_world.ru -p 3010. I then opened up the web browser page at http://127.0.0.1:3010/ and observered the "Hello World! message.

Resources