代码之家  ›  专栏  ›  技术社区  ›  TK.

使用sinatra提供静态文件

  •  136
  • TK.  · 技术社区  · 16 年前

    我有一个网页网站只使用html,css和javascript。我想把这个应用程序部署到Heroku,但是我找不到方法。我现在正试图使这个应用程序与sinatra一起工作。

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

    以下是 myapp.rb .

    require 'rubygems'
    require 'sinatra'
    
    get "/" do
      # What should I write here to point to the `index.html`
    end
    
    14 回复  |  直到 8 年前
        1
  •  127
  •   Tate Johnson    16 年前

    在没有任何附加配置的情况下,sinatra将在 public . 对于空路由,您需要呈现索引文档。

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

    路线应该返回 String 它成为http响应体。 File.read 打开文件,读取文件,关闭文件并返回 弦 .

        2
  •  164
  •   Chad DeShon    14 年前

    你可以使用 send_file 提供文件的助手。

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

    这会有用的 index.html 从任何配置为包含应用程序静态文件的目录。

        3
  •  61
  •   Morgan    14 年前

    您可以从公用文件夹托管它们,它们不需要路由。

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

    在myapp.rb中

    set :public_folder, 'public'
    
    get "/" do
      redirect '/index.html'
    end
    

    链接到公用子文件夹

    set :public_folder, 'public'
    get "/" do
      redirect '/subfolder/index.html' 
    end
    

    /public中的所有内容都可以从'/whatever/bla.html访问

    例子:
    ./public/stylesheets/screen.css/公共/stylesheets/screen.css
    可通过“/stylesheets/screen.css”访问,无需路由

        4
  •  12
  •   Rob Cameron    15 年前

    请记住,在生产中,您可以让您的web服务器发送 index.html 自动地,这样请求就永远不会到达sinatra。这对性能更有利,因为您不必通过sinatra/rack堆栈来提供静态文本,这正是apache/nginx所擅长的。

        5
  •  12
  •   Giacomo1968 Arvind    12 年前

    sinatra应该允许您提供来自公共目录的静态文件 as explained in the docs :

    静态文件

    静态文件从./public目录提供。可以通过设置:public选项指定其他位置:

    注意,url中不包括公共目录名。文件./public/css/style.css作为example.com/css/style.css提供。

        6
  •  2
  •   jitter    12 年前

    这个 sinatra-assetpack gem提供了很多特性。语法很好:

    serve '/js', from: '/app/javascripts'
    

    当我仍然对rails资产管道有问题时,我觉得我可以使用 西纳特拉资产包 -但大多数情况下,它只处理几行代码。

        7
  •  2
  •   z atef    11 年前

    更新的答案 : 我把上面的所有内容都绑定在一起,却没能加载css、js…等内容加载的唯一东西是index.html…剩下的人就要去了。 404 error

    我的解决方案:应用程序文件夹如下所示。

    index.rb ==>>sinatra代码生效。

    require 'rubygems'
    require 'sinatra'
    
    get '/' do
      html :index
    end
    
    def html(view)
      File.read(File.join('public', "#{view.to_s}.html"))
    end
    

    public folder ==>>包含所有其他内容…css、js、blah blah…等。

    user@user-SVE1411EGXB:~/sintra1$ ls
    index.rb  public
    user@user-SVE1411EGXB:~/sintra1$ find public/
    public/
    public/index.html
    public/about_us.html
    public/contact.html
    public/fonts
    public/fonts/fontawesome-webfont.svg
    public/fonts/fontawesome-webfont.ttf
    public/img
    public/img/drink_ZIDO.jpg
    public/js
    public/js/bootstrap.min.js
    public/js/jquery.min.js
    public/js/bootstrap.js
    public/carsoul2.html
    public/css
    public/css/font-awesome-ie7.css
    public/css/bootstrap.min.css
    public/css/font-awesome.min.css
    public/css/bootstrap.css
    public/css/font-awesome.css
    public/css/style.css
    user@user-SVE1411EGXB:~/sintra1$
    

    现在启动服务器,您就可以毫无问题地浏览静态页面了。

    user@user-SVE1411EGXB:~/sintra1$ ruby index.rb 
    == Sinatra/1.4.5 has taken the stage on 4567 for development with backup from Thin
    >> Thin web server (v1.5.1 codename Straight Razor)
    >> Maximum connections set to 1024
    >> Listening on localhost:4567, CTRL+C to stop
    
        8
  •  2
  •   Muhammad Aamir Talib    11 年前

    在主rb文件的下面一行添加

    set :public_folder, 'public'
    
        9
  •  2
  •   Vlad Hilko    11 年前
    require 'rubygems'
    require 'sinatra'
    
    set :public_folder, File.dirname(__FILE__) + '/../client'
    #client - it's folder with all your file, including myapp.rb
    
    get "/" do
      File.read('index.html')
    end
    
        10
  •  1
  •   bonh R.C    12 年前

    你可以考虑把 index.html 归档到 views/index.erb ,并定义一个端点,如:

    get '/' do
      erb :index
    end
    
        11
  •  0
  •   Charmi    11 年前

    放入文件 public 文件夹有限制。实际上,当你在根 '/' 路径工作正常,因为浏览器将设置css文件的相对路径,例如 /css/style.css 西纳特拉会在 公众的 目录。但是,如果您的位置是 /user/create ,则Web浏览器将在 /user/create/css/style.css 会失败的。

    作为解决方法,我添加了以下重定向以正确加载css文件:

    get %r{.*/css/style.css} do
        redirect('css/style.css')
    end
    
        12
  •  0
  •   Hayden    8 年前

    http://sinatrarb.com/configuration.html#static---enabledisable-static-file-routes

    这是正确的做法。

    set :public_folder, 'public'
    

    我使用了静态设置,因为它的设置会影响公用文件夹的使用。

        13
  •  0
  •   r0ma    8 年前

    您始终可以使用rack::static

    https://www.rubydoc.info/gems/rack/Rack/Static

    只需在“run”命令之前将这一行添加到“config.ru”

    use Rack::Static, :urls => [""], :root => 'public', :index => 'index.html'
    
        14
  •  -7
  •   heldopslippers    16 年前

    这个解决方案怎么样?:

    get "/subdirectory/:file" do 
      file = params[:file] + "index.html"
      if File.exists?(params[:file])
        return File.open("subdirectory/" + file)
      else
       return "error"
      end
    end
    

    因此,如果现在导航到(例如)/subdirectory/test/it将加载subdirectory/test/index.html