使用 Sinatra 提供静态文件 [英] Serving static files with Sinatra

查看:26
本文介绍了使用 Sinatra 提供静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个仅使用 HTML、CSS 和 JavaScript 的单页网站.我想将该应用程序部署到 Heroku,但找不到方法.我现在正在尝试使该应用与 Sinatra 配合使用.

<预><代码>.|-- 应用程序.css|-- application.js|-- index.html|-- jquery.js`-- myapp.rb

以下是myapp.rb的内容.

需要'rubygems'需要'sinatra'得到/"做# 我应该在这里写什么来指向`index.html`结尾

解决方案

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

需要'rubygems'需要'sinatra'得到'/'做File.read(File.join('public', 'index.html'))结尾

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

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

解决方案

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.

这篇关于使用 Sinatra 提供静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆