可能重复:
使用Sinatra提供静态文件
我注意到Sinatra无法识别公用文件夹子目录中的index.html文件,并在url指向目录时返回错误而未指定文件名.
例如,如果用户输入"www.mydomain.com/subdiretory/"之类的网址,则Sinatra无法识别该目录中是否存在index.html文件.
我的公用文件夹中有数百个子目录,因此无法在代码中指定每个子目录(并且子目录的数量不断增长).
如果url指向没有文件名的目录,如果公共文件夹的子目录中有index.html文件,我怎么能告诉Sinatra单独离开我的Web服务器(Apache)(到服务器index.html文件)?
enable :sessions
get '/foo' do
session['m'] = 'Hello World!'
redirect '/bar'
end
get '/bar' do
session['m'] # => 'Hello World!'
end
Run Code Online (Sandbox Code Playgroud)
它似乎不起作用.
我一直在读书,我发现这个名为Grape的微框架用于红宝石.我目前正在使用Sinatra来处理Web界面,但我还想实现Grape来处理应用程序的API方面.我找不到任何有用的建议来解决这个问题.葡萄文档说"Grape是一个类似REST的API微框架,用于Ruby.它设计用于在Rack上运行,或通过提供简单的DSL来轻松开发RESTful API,从而补充现有的Web应用程序框架,如Rails和Sinatra." 所以听起来应该有正式的两种方式相结合的方式吗?这个应用程序也将在Heroku上运行.
我正在尝试在我的应用程序中利用Server-Sent Events.我正在使用Sinatra和sinatra-sse宝石.这个宝石包裹着Sinatra的stream :keep_alive召唤.
在Thin上运行我的应用程序时,我绝对没有问题,我的事件流按预期工作.然而,当我切换我的应用程序与Puma一起运行时,一切正常,除了我sse_stream绝对没有!它只返回一个空白页面.
我的流设置如此
get "/logstream/:server" do
if rbcserver = MyApp.servers[params[:server]]
sse_stream do |stream|
rbcserver.add_web_logger(stream)
stream.callback { rbcserver.remove_web_logger(stream) }
end
else
error 404
end
end
Run Code Online (Sandbox Code Playgroud)
我这样开始瘦:
@@puma_instance = Puma::Server.new MyApp::WebUI
@@puma_instance.add_tcp_listener ip, port
@@puma_instance.run
Run Code Online (Sandbox Code Playgroud)
知道发生了什么事吗?任何帮助,将不胜感激.
编辑:更多信息这是cURL在Puma上运行时提供的
$ curl -L -b cookies.txt -c cookies.txt -i http://localhost:9001/logstream/myserver
HTTP/1.1 200 OK
Content-Type: text/event-stream;charset=utf-8
X-Content-Type-Options: nosniff
Transfer-Encoding: chunked
$
Run Code Online (Sandbox Code Playgroud)
而这就是Thin上发生的事情
$ curl -L -b cookies.txt -c cookies.txt -i http://localhost:9001/logstream/myserver
HTTP/1.1 200 OK
Content-Type: text/event-stream;charset=utf-8
X-Content-Type-Options: nosniff
Connection: …Run Code Online (Sandbox Code Playgroud) 在将模块化Sinatra应用程序部署到Heroku时,我不断收到此错误:
bundler: failed to load command: rackup (/app/vendor/bundle/ruby/2.4.0/bin/rackup)
我试过调整我的config.ru和我的Procfile,没有成功.以下是config.ru的内容:
$LOAD_PATH.unshift(::File.expand_path('lib', ::File.dirname(__FILE__)))
require_relative 'lib/app'
run Tir::App
Run Code Online (Sandbox Code Playgroud)
Procfile:
web: bundle exec rackup config.ru -p $PORT
Run Code Online (Sandbox Code Playgroud)
LIB/app.rb:
require 'sinatra/base'
module Tir
class App < Sinatra::Base
# code
end
end
Run Code Online (Sandbox Code Playgroud)
请指出我正确的方向.
PS.我可以部署应用程序,但在尝试使用Pony gem发送电子邮件时会出现上述错误.
编辑20.07.2018: 我已经调试了这个问题并找到导致崩溃的线路:
https://gist.github.com/abbottjam/248edfd50f094f99b3bf5b1f995fd290#file-static-rb-L15
错误消息现在说:
内部服务器错误:没有将Array隐式转换为String
所以它必须是configure_options下面方法中的代码块.仍然不知道数组 - >字符串转换应该在哪里发生.谢谢.
是否有适合Windows的ruby易于配置的网络服务器?
我希望找到一个可以轻松配置为与Ruby on Rails或Sinatra配合使用的Web服务器.
有人知道吗?
我有一个接受JSON的post方法:
post '/channel/create' do
content_type :json
@data = JSON.parse(env['rack.input'].gets)
if @data.nil? or !@data.has_key?('api_key')
status 400
body({ :error => "JSON corrupt" }.to_json)
else
status 200
body({ :error => "Channel created" }.to_json)
end
Run Code Online (Sandbox Code Playgroud)
作为rspec的新手,我很困惑,试图找出如何使用可接受的JSON有效负载编写针对该POST的测试.我最接近的是这是非常不准确但我似乎没有问谷歌上帝正确的问题,以帮助我在这里.
it "accepts create channel" do
h = {'Content-Type' => 'application/json'}
body = { :key => "abcdef" }.to_json
post '/channel/create', body, h
last_response.should be_ok
end
Run Code Online (Sandbox Code Playgroud)
在Sinatra中测试API的任何最佳实践指南也将非常受欢迎.
我已经将运行VistualBox的Vagrant(1.2.2)VM设置为:private_network,我已经启动了一个Sinatra服务器.但是我无法连接到那个Sinatra实例.然而,VM运行并响应ping.
这是我的Vagrantfile
Vagrant.configure("2") do |config|
config.vm.box = "precise64"
config.vm.network :private_network, ip: "192.168.33.10"
end
Run Code Online (Sandbox Code Playgroud)
所以我启动了Vagrant VM并将其添加到其中
prodserv$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Setting the name of the VM...
[default] Clearing any previously set forwarded ports...
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] Booting VM...
[default] Waiting for VM to boot. This …Run Code Online (Sandbox Code Playgroud) 用两者启动我的Sinatra应用程序后ruby app.rb,foreman start我无法使用localhost和主机上的相应端口访问我的应用程序.我也可以curl从来宾机器的shell中的应用程序,而在主机上curl请求失败.据我所知,客户机上不应该有防火墙,因为我正在使用Vagrant Ubuntu映像.
我的Vagrantfile如下:
Vagrant.configure('2') do |config|
config.vm.box = 'precise32'
config.vm.box_url = 'http://files.vagrantup.com/precise32.box'
config.vm.network :forwarded_port, guest: 4567, host: 4567
end
Run Code Online (Sandbox Code Playgroud) 非常第一行写sinatra的是用Ruby以最小的代价快速创建web应用的DSL.我可以理解它重量轻,非常灵活,可以快速创建Web应用程序并且只需很少的工作量但无法理解它是如何成为DSL的?