Sinatra로 정적 파일 제공
저는 HTML, CSS, JavaScript만을 사용하는 한 페이지 웹사이트를 가지고 있습니다.헤로쿠에 앱을 배포하고 싶은데 방법을 찾을 수 없습니다.저는 지금 시나트라와 함께 작동하는 앱을 만들기 위해 노력하고 있습니다.
.
|-- 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
사용할 수 있습니다.send_file파일을 제공하는 도우미입니다.
require 'sinatra'
get '/' do
send_file File.join(settings.public_folder, 'index.html')
end
이것은 도움이 될 것입니다.index.html응용프로그램의 정적 파일이 있는 것으로 구성된 디렉토리에서 사용할 수 있습니다.
추가 구성 없이 Sinatra는 자산을 제공할 것입니다.public빈 경로의 경우 색인 문서를 렌더링할 수 있습니다.
require 'rubygems'
require 'sinatra'
get '/' do
File.read(File.join('public', 'index.html'))
end
경로는 다음을 반환해야 합니다.StringHTTP 응답 본문이 됩니다. File.read파일을 열고, 파일을 읽고, 파일을 닫고, 파일을 반환합니다.String.
공용 폴더에서 호스트하기만 하면 되며 경로가 필요하지 않습니다.
.
-- 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의 모든 항목은 '/what/bla.html'에서 액세스할 수 있습니다.
예:
./공용/스타일시트/스크린.vmdk
'/stylesheets/screen.css'를 통해 액세스할 수 있습니다. 경로가 필요하지 않습니다.
운영 환경에서 웹 서버를 전송할 수 있습니다.index.html요청이 시나트라에게 전달되지 않도록 자동으로.정적 텍스트를 제공하기 위해 Sinatra/Rack 스택을 거칠 필요가 없으므로 성능이 향상됩니다. 이는 Apache/Nginx의 탁월한 기능입니다.
Sinatra는 문서에 설명된 대로 공용 디렉토리에서 정적 파일을 제공할 수 있도록 해야 합니다.
정적 파일
정적 파일은 ./public 디렉토리에서 제공됩니다.:public 옵션을 설정하여 다른 위치를 지정할 수 있습니다.
공용 디렉토리 이름은 URL에 포함되지 않습니다../public/syslogs/style.dll 파일을 example.com/css/style.css 로 사용할 수 있습니다.
메인 rb 파일에 아래 줄 추가
set :public_folder, 'public'
참조: http://sinatrarb.com/configuration.html#static---enabledisable-static-file-routes
http://sinatrarb.com/configuration.html#static---enabledisable-static-file-routes
이것이 올바른 방법일 것입니다.
set :public_folder, 'public'
정적 설정은 public_folder 사용에 영향을 줄 수 있기 때문에 사용했습니다.
랙은 언제든지 사용할 수 있습니다.:정적
https://www.rubydoc.info/gems/rack/Rack/Static
'config.ru '에 'run' 명령 앞에 이 행을 추가하기만 하면 됩니다.
use Rack::Static, :urls => [""], :root => 'public', :index => 'index.html'
시나트라-트래픽 팩 보석은 많은 특징들을 제공합니다.구문은 달콤합니다.
serve '/js', from: '/app/javascripts'
저는 여전히 철도 자산 파이프라인에 문제가 있는 동안 시나트라-트래픽 팩을 사용하여 훨씬 더 많은 제어를 할 수 있다고 생각합니다. 하지만 대부분의 경우 몇 줄의 코드로만 작동합니다.
업데이트된 답변:나는 위의 모든 것을 연결했지만, 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
public folder==>>에는 다른 모든 것이 포함되어 있습니다. 예를 들어, 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
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
이동하는 것을 고려해 볼 수 있습니다.index.html을 로다철하에 합니다.views/index.erb엔드포인트를 다음과 같이 정의합니다.
get '/' do
erb :index
end
파일 저장 위치시키는 중public폴더에 제한이 있습니다.루트 때는 당이뿌있에을때리신실▁actually.'/'를 들어, 합니다./css/style.css그리고 시나트라는 그 파일을 찾을 것입니다.public디렉토리입니다.그러나 사용자의 위치가 예를 들어/user/create웹▁css▁file에서 당신의 css 파일을 찾을 것입니다./user/create/css/style.css그리고 실패할 것입니다.
해결 방법으로 CSS 파일을 올바르게 로드하기 위해 다음과 같은 리디렉션을 추가했습니다.
get %r{.*/css/style.css} do
redirect('css/style.css')
end
이 솔루션은 어떻습니까?:
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.dll이 로드됩니다.
언급URL : https://stackoverflow.com/questions/2437390/serving-static-files-with-sinatra
'programing' 카테고리의 다른 글
| psql을 사용하여 데이터베이스에 설치된 확장을 어떻게 나열합니까? (0) | 2023.06.03 |
|---|---|
| 루비 수면? 아니면 1초 미만의 지연? (0) | 2023.06.03 |
| Git 하위 모듈과 하위 트리 간의 차이 (0) | 2023.06.03 |
| UITableView 행 애니메이션 기간 및 완료 콜백 (0) | 2023.06.03 |
| Git에서 로컬 커밋을 폐기하는 방법은 무엇입니까? (0) | 2023.06.03 |