28

I have a simple sinatra application. All I want to do is use it as a wrapper to serve a static HTML file at a specific route. My directory structure looks like this:

/directory
    myhtmlfile.html
    app.rb

My app.rb file looks like this:

require 'sinatra'

get '/myspecialroute' do
    html :myhtmlfile      # i know html is not a method, but this is what I would like to do
end

How can I write this so that I can keep my html file a plain html file but serve it at a special route?

Solution:

Thanks to this, I learned a few different ways to do it:

get '/myspecialroute' do
  File.read('myhtmlfile.html')
end

This will open, read, close, then return the file as a string.

Or there is a helper function to make this cleaner:

get '/myspecialroute' do
  send_file 'myhtmlfile.html'
end
Community
  • 1
  • 1
Andrew
  • 227,796
  • 193
  • 515
  • 708
  • You could change it to .erb and then call the erb method with no erb in it. – Uri Nov 03 '11 at 15:07
  • Do you want it to render the HTML file when requesting `'/myspecialroute'` or `'/myspecialroute.html'`? – tbuehlmann Nov 03 '11 at 15:16
  • @tbuehlmann I would like the route to stay `/myspecialroute` – Andrew Nov 03 '11 at 15:39
  • If all you're using Sinatra for is serving a static HTML file, why are you using Sinatra at all? – Marnen Laibow-Koser Nov 03 '11 at 17:04
  • 1
    @MarnenLaibow-Koser To start up a zero-configuration web server on an available port. Also to add a special route. Do you have any alternatives? – Andrew Nov 03 '11 at 17:07
  • I thought you meant that you'd only be serving the one static file. If that's the case, how about [pow](http://pow.cx/) or some other simple server? – Marnen Laibow-Koser Nov 03 '11 at 17:31
  • @MarnenLaibow-Koser Pow is cool. I use that for local development. But I didn't want it to be on port 80. Also, I wanted to share this page with my co-workers. So they could go to http://my.ip.address:4567/myspecialroute – Andrew Nov 03 '11 at 17:34
  • Is https://github.com/37signals/pow/issues/60 helpful? – Marnen Laibow-Koser Nov 03 '11 at 17:40
  • @MarnenLaibow-Koser If I could set a different port in a project specific config file, it would be perfect, but I don't see a way to do that out of the box. – Andrew Nov 03 '11 at 18:11
  • Hmm. There's gotta be a lightweight server that can do that. Sinatra seems like overkill if you're not using server-side logic. – Marnen Laibow-Koser Nov 03 '11 at 18:13
  • 1
    How about WEBrick? I know it somehow seems passé, but it might be what you need here. See http://onestepback.org/index.cgi/Tech/Ruby/WEBrick.rdoc . – Marnen Laibow-Koser Nov 03 '11 at 18:16

3 Answers3

45

Does send_file do what you want?

e.g.

  get '/myspecialroute' do
     send_file 'special.html'
  end
Steve
  • 15,606
  • 3
  • 44
  • 39
3

You can do it like this:

get '/myspecialroute' do
  redirect '/myspecialroute.html'
end
Uri
  • 2,306
  • 2
  • 24
  • 25
1

This is work for me:

require 'rubygems'
require 'sinatra'

get '/index.html' do
    @page_title = 'Home'
    @page_id = 'index.html'
    erb :'index.html', { :layout => :'layout.html' }
end