首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Ruby Rack -挂载一个默认读取index.html的简单web服务器

Ruby Rack -挂载一个默认读取index.html的简单web服务器
EN

Stack Overflow用户
提问于 2010-10-05 20:54:29
回答 4查看 10.5K关注 0票数 19

我正在尝试从本教程中获得一些信息:http://m.onkey.org/2008/11/18/ruby-on-rack-2-rack-builder

基本上,我希望有一个文件config.ru,告诉rack读取当前目录,这样我就可以像访问简单的apache服务器一样访问所有文件,还可以使用index.html file...is读取默认根目录。有什么方法可以做到吗?

我当前的config.ru如下所示:

代码语言:javascript
复制
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不能正常启动。

代码语言:javascript
复制
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

提前感谢

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2010-10-14 14:23:39

我想你错过了rackup命令。下面是它的用法:

代码语言:javascript
复制
rackup config.ru

这将使用webrick在端口9292上运行您的rack应用程序。你可以阅读"rackup --help“来获得更多关于如何改变这些默认值的信息。

关于您要创建的应用程序。下面是我认为它应该是什么样子:

代码语言:javascript
复制
# 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
}
票数 38
EN

Stack Overflow用户

发布于 2013-09-13 11:46:28

我最后在这一页上找了一条单行...

如果您想要的只是为几个一次性任务提供当前目录,那么这就是您所需要的:

代码语言:javascript
复制
ruby -run -e httpd . -p 5000

有关它的工作原理的详细信息:http://www.benjaminoakes.com/2013/09/13/ruby-simple-http-server-minimalist-rake/

票数 10
EN

Stack Overflow用户

发布于 2012-07-31 23:47:24

您可以使用Rack::Static来完成此操作

代码语言:javascript
复制
map "/foo" do
  use Rack::Static, 
    :urls => [""], :root => File.expand_path('bar'), :index => 'index.html'
  run lambda {|*|}
end
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3863781

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档