标签:style http io 使用 ar sp on 代码 html
一、什么是Rack?
rack 实际上是一种api。它用最简单的方式封装了http请求和响应,是统一和提炼了服务器和框架,以及两者之间的软件(中间件)的api(借口)。
二、rack的作用:
三、rack 的规定:
返回一个返回参数(enviroment)的call()函数;
call()函数return一个数组[http_status_code, responde_header_hash, body]
四、rack的实例解析:
1、
require ‘rubygems‘
require ‘rack‘
class HelloWorld
def call(env)
[200, {"Content-Type" => "text/html"}, "Hello Rack!"]
end
end
Rack::Handler::Mongrel.run HelloWorld.new, :Port => 9292
通过传递HelloWorld的一个对象到mongrel rack handler ,然后启动了服务端口9292
2、
require ‘rubygems‘ require ‘rack‘ Rack::Handler::Mongrel.run proc {|env| [200, {"Content-Type" => "text/html"}, "Hello Rack!"]}, :Port => 9292
应为返回的是一个call()函数,可以用proc来实现代码
3、require ‘rubygems‘require ‘rack‘
def application(env) [200, {"Content-Type" => "text/html"}, "Hello Rack!"] end
Rack::Handler::Mongrel.run method(:application), :Port => 9292使用方法类来实现
五、安装了rack gem
还可以这样实现上面的代码:
在config.ru
run Proc.new {|env| [200, {"Content-Type" => "text/html"}, "Hello Rack!"]}
然后运行:
rackup config.ru
标签:style http io 使用 ar sp on 代码 html
原文地址:http://www.cnblogs.com/chenzhenzhen/p/4017726.html