使用 Sinatra 提供静态文件

发布于 2024-08-25 12:37:54 字数 380 浏览 4 评论 0原文

我有一个仅使用 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

千笙结 2024-09-01 12:37:54

您可以使用 send_file 帮助程序来提供文件。

require 'sinatra'

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

这将从任何已配置为包含应用程序静态文件的目录中提供 index.html 服务。

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.

琉璃梦幻 2024-09-01 12:37:54

无需任何额外配置,Sinatra 将在 public 中提供资产。对于空路由,您需要渲染索引文档。

require 'rubygems'
require 'sinatra'

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

路由应该返回一个 String ,它成为 HTTP 响应正文。 File.read 打开一个文件,读取该文件,关闭该文件并返回一个String

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.

行至春深 2024-09-01 12:37:54

您可以仅从公共文件夹托管它们,并且它们不需要路由。

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

在 myapp.rb 中

set :public_folder, 'public'

get "/" do
  redirect '/index.html'
end

链接到 public 中的某个子文件夹

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

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

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

You could just host them from the public folder and they do not need routes.

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

In the myapp.rb

set :public_folder, 'public'

get "/" do
  redirect '/index.html'
end

Link to some sub folder in public

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

Everything in ./public is accessible from '/whatever/bla.html

Example :
./public/stylesheets/screen.css
Will be accessible via '/stylesheets/screen.css' no route required

枉心 2024-09-01 12:37:54

请记住,在生产中,您可以让 Web 服务器自动发送 index.html,这样请求就永远不会到达 Sinatra。这对于性能来说更好,因为您不必仅仅为了提供静态文本而通过 Sinatra/Rack 堆栈,而这正是 Apache/Nginx 所擅长的。

Keep in mind that in production you can have your web server send out index.html automatically so that the request never gets to Sinatra. This is better for performance as you don't have to go through the Sinatra/Rack stack just to serve static text, which is what Apache/Nginx are awesome at doing.

但可醉心 2024-09-01 12:37:54

Sinatra 应该允许您从公共目录提供静态文件如文档中所述

静态文件

静态文件由 ./public 目录提供。您可以通过设置 :public 选项来指定不同的位置:

请注意,公共目录名称不包含在 URL 中。文件 ./public/css/style.css 作为 example.com/css/style.css 提供。

Sinatra should let you serve static files from the public directory as explained in the docs:

Static Files

Static files are served from the ./public directory. You can specify a different location by setting the :public option:

Note that the public directory name is not included in the URL. A file ./public/css/style.css is made available as example.com/css/style.css.

强辩 2024-09-01 12:37:54

在主 rb 文件引用中添加以下行

set :public_folder, 'public'

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

Add below line in main rb file

set :public_folder, 'public'

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

A君 2024-09-01 12:37:54

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

这将是正确的方法。

set :public_folder, 'public'

我使用静态设置,因为它的设置会影响 public_folder 的使用。

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

This would be the correct way of doing it.

set :public_folder, 'public'

I used the static setting because it's setting can affect the public_folder usage.

靖瑶 2024-09-01 12:37:54

您始终可以使用 Rack::Static

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

只需在“config.ru”中的“run”命令之前添加此行

use Rack::Static, :urls => [""], :root => 'public', :index => 'index.html'

You can always use Rack::Static

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

Just add this line before 'run' command into 'config.ru'

use Rack::Static, :urls => [""], :root => 'public', :index => 'index.html'
深陷 2024-09-01 12:37:54

sinatra-assetpack gem 提供了一大堆功能。语法很不错:

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

虽然我仍然遇到 Rails 资产管道问题,但我觉得使用 sinatra- 可以更好地控制assetpack - 但大多数时候它只需要几行代码即可工作。

the sinatra-assetpack gem offers a whole bunch of features. syntax is sweet:

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

while i am still having issues with rails assets pipeline i feel like i have much more control using sinatra-assetpack - but most of the times it just works with a few lines of code.

听不够的曲调 2024-09-01 12:37:54

更新答案
我将上述所有内容绑定在一起,但没有运气能够加载 css、js....等内容,唯一加载的是index.html...其余的都在 =>> 404 error

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

index.rb ==>>西纳特拉代码如下。

require 'rubygems'
require 'sinatra'

get '/' do
  html :index
end

def html(view)
  File.read(File.join('public', "#{view.to_s}.html"))
end

公共文件夹==>>包含其他所有内容...css、js、等等。

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

UPDATED ANSWER:
I tied all the above with no luck of being ablle to load css, js....etc contents the only thing that was loading is index.html... and the rest were going =>> 404 error

My solution: app folder looks like this .

index.rb ==>> Sinatra code goes .

require 'rubygems'
require 'sinatra'

get '/' do
  html :index
end

def html(view)
  File.read(File.join('public', "#{view.to_s}.html"))
end

public folder==>> contains everything else ...css , js , blah blah..etc.

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$

Now start server and you will be able to navigate through static pages with no problem.

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
春花秋月 2024-09-01 12:37:54
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
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
此岸叶落 2024-09-01 12:37:54

您可以考虑将 index.html 文件移动到 views/index.erb,并定义一个端点,例如:

get '/' do
  erb :index
end

You might consider moving the index.html file to views/index.erb, and defining an endpoint like:

get '/' do
  erb :index
end
束缚m 2024-09-01 12:37:54

将文件放入 public 文件夹有一个限制。实际上,当您位于根 '/' 路径时,它可以正常工作,因为浏览器将设置 css 文件的相对路径,例如 /css/style.css 和sinatra 将在 public 目录中查找该文件。但是,如果您的位置例如是 /user/create,那么 Web 浏览器将在 /user/create/css/style.css 中查找您的 css 文件,并会失败。

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

get %r{.*/css/style.css} do
    redirect('css/style.css')
end

Putting files in public folder has a limitation. Actually, when you are in the root '/' path is works correctly because the browser will set the relative path of your css file for example /css/style.css and sinatra will look for the file in the public directory. However, if your location is for example /user/create, then the web browser will look for your css file in /user/create/css/style.css and will the fail.

As a workaround, I added the following redirection to correctly load css file:

get %r{.*/css/style.css} do
    redirect('css/style.css')
end
孤星 2024-09-01 12:37:54

这个解决方案怎么样? :

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/,它将加载 subdirectory/test/index.html

What about this solution? :

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

so if you now navigate to (for example) /subdirectory/test/ it will load subdirectory/test/index.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文