Serving static files with Sinatra

Without any additional configuration, Sinatra will serve assets in public. For the empty route, you'll want to render the index document.

require 'rubygems'
require 'sinatra'

get '/' do
  File.read(File.join('public', 'index.html'))
end

Routes should return a String which become the HTTP response body. File.read opens a file, reads the file, closes the file and returns a String.


You can use the send_file helper to serve files.

require 'sinatra'

get '/' do
  send_file File.join(settings.public_folder, 'index.html')
end

This will serve index.html from whatever directory has been configured as having your application's static files.

Tags:

Ruby

Sinatra