Sinatra - 在每个请求中渲染index.html

时间:2014-11-28 15:31:03

标签: angularjs sinatra assets foreman

我现在很难过,这是我的应用程序昨天工作正常的时刻之一,但突然间它没有,我不知道为什么(我和#39;我相信你可以联系。)

module SK
  module Routes
    class Base < Sinatra::Base
      include Models

      get '/*' do
        File.read 'public/index.html'
      end

      helpers Helpers::API
    end
  end
end

我正在构建一个Angular应用,所以我需要在每个请求上提供index.html。当我使用shotgun gem时,一切正常,但只要我使用foreman将其投入生产,它就会将所有内容(包括资产)作为index.html提供。

这是我的Procfile:

web: bundle exec rackup config.ru -p $PORT

这是我的config.ru:

require './app'

run SK::App

所以我不明白为什么它在开发中有效,但在生产中却没有。

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

Shotgun将使用its own static server(以避免在不需要时分叉)。如果配置为Sinatra还将提供静态文件,但using the modular style app disables this by default

修复方法是启用静态服务器:

module SK
  module Routes
    class Base < Sinatra::Base

      # add this line:
      enable :static

      include Models

      get '/*' do
        File.read 'public/index.html'
      end

      helpers Helpers::API
    end
  end
end

现在,在尝试匹配路线之前,Sinatra将从公共目录提供静态文件。

相关问题