我正在尝试从本教程中获得一些信息:http://m.onkey.org/2008/11/18/ruby-on-rack-2-rack-builder
基本上,我希望有一个文件config.ru
,告诉rack读取当前目录,这样我就可以像访问简单的apache服务器一样访问所有文件,还可以使用index.html file...is读取默认根目录。有什么方法可以做到吗?
我当前的config.ru
如下所示:
run Rack::Directory.new('')
#this would read the directory but it doesn't set the root to index.html
map '/' do
file = File.read('index.html')
run Proc.new {|env| [200, {'Content-Type' => 'text/html'}, file] }
end
#using this reads the index.html mapped as the root but ignores the other files in the directory
所以我不知道该怎么做...
我也按照教程示例进行了尝试,但thin
不能正常启动。
builder = Rack::Builder.new do
run Rack::Directory.new('')
map '/' do
file = File.read('index.html')
run Proc.new {|env| [200, {'Content-Type' => 'text/html'}, file] }
end
end
Rack::Handler::Thin.run builder, :port => 3000
提前感谢
发布于 2010-10-14 14:23:39
我想你错过了rackup命令。下面是它的用法:
rackup config.ru
这将使用webrick在端口9292上运行您的rack应用程序。你可以阅读"rackup --help“来获得更多关于如何改变这些默认值的信息。
关于您要创建的应用程序。下面是我认为它应该是什么样子:
# This is the root of our app
@root = File.expand_path(File.dirname(__FILE__))
run Proc.new { |env|
# Extract the requested path from the request
path = Rack::Utils.unescape(env['PATH_INFO'])
index_file = @root + "#{path}/index.html"
if File.exists?(index_file)
# Return the index
[200, {'Content-Type' => 'text/html'}, File.read(index_file)]
# NOTE: using Ruby >= 1.9, third argument needs to respond to :each
# [200, {'Content-Type' => 'text/html'}, [File.read(index_file)]]
else
# Pass the request to the directory app
Rack::Directory.new(@root).call(env)
end
}
发布于 2013-09-13 11:46:28
我最后在这一页上找了一条单行...
如果您想要的只是为几个一次性任务提供当前目录,那么这就是您所需要的:
ruby -run -e httpd . -p 5000
有关它的工作原理的详细信息:http://www.benjaminoakes.com/2013/09/13/ruby-simple-http-server-minimalist-rake/
发布于 2012-07-31 23:47:24
您可以使用Rack::Static来完成此操作
map "/foo" do
use Rack::Static,
:urls => [""], :root => File.expand_path('bar'), :index => 'index.html'
run lambda {|*|}
end
https://stackoverflow.com/questions/3863781
复制相似问题