Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Sinatra, how can I serve static index.html files in subdirectories in public folder? [duplicate]

Tags:

ruby

sinatra

I have one page website only using HTML, CSS and JavaScript. I want to deploy the app to Heroku, but I cannot find a way to do it. I am now trying to make the app working with Sinatra.

.
|-- application.css
|-- application.js
|-- index.html
|-- jquery.js
`-- myapp.rb

And the following is the content of myapp.rb.

require 'rubygems'
require 'sinatra'

get "/" do
  # What should I write here to point to the `index.html`
end
like image 373
TK. Avatar asked Mar 13 '10 05:03

TK.


2 Answers

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.

like image 120
Ryan Ahearn Avatar answered Oct 15 '22 16:10

Ryan Ahearn


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.

like image 24
Tate Johnson Avatar answered Oct 15 '22 15:10

Tate Johnson