Sinatra로 정적 파일 제공
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
추가 구성없이 Sinatra는에 자산을 제공 public합니다. 빈 경로의 경우 인덱스 문서를 렌더링하려고합니다.
require 'rubygems'
require 'sinatra'
get '/' do
File.read(File.join('public', 'index.html'))
end
경로는 StringHTTP 응답 본문이 되는를 반환해야합니다 . File.read파일을 열고, 파일을 읽고, 파일을 닫고 a를 반환합니다 String.
send_file헬퍼를 사용하여 파일을 제공 할 수 있습니다.
require 'sinatra'
get '/' do
send_file File.join(settings.public_folder, 'index.html')
end
이것은 index.html응용 프로그램의 정적 파일을 갖도록 구성된 디렉토리에서 제공 됩니다.
공용 폴더에서 호스트 할 수 있으며 경로가 필요하지 않습니다.
.
-- 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'를 통해 액세스 할 수 있습니다.
프로덕션 환경 index.html에서는 요청이 Sinatra에 전달되지 않도록 웹 서버를 자동으로 보낼 수 있습니다 . 정적 텍스트를 제공하기 위해 Sinatra / Rack 스택을 거치지 않아도되므로 성능이 더 좋습니다. 이는 Apache / Nginx가하는 일 중 최고입니다.
Sinatra는 docs에 설명 된대로 공개 디렉토리에서 정적 파일을 제공 할 수 있도록해야합니다 .
정적 파일
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.
Add below line in main rb file
set :public_folder, 'public'
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.
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
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
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.
You might consider moving the index.html file to views/index.erb, and defining an endpoint like:
get '/' do
erb :index
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
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'
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
참고URL : https://stackoverflow.com/questions/2437390/serving-static-files-with-sinatra
'IT' 카테고리의 다른 글
| 더 이상 사용되지 않는 것으로 C ++ 표시 (0) | 2020.06.22 |
|---|---|
| 다음 모듈은 최적화를 사용하거나 디버그 정보를 사용하지 않고 구축되었습니다. (0) | 2020.06.22 |
| 목록 교차점을 찾는 방법은 무엇입니까? (0) | 2020.06.22 |
| 쉼표로 구분 된 문자열로 목록 변환 (0) | 2020.06.22 |
| Java는 컴파일 된 또는 해석 된 프로그래밍 언어입니까? (0) | 2020.06.22 |