首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

rack 引见

2012-12-20 
rack 介绍?rack是什么?Rack provides a minimal, modular and adaptable interface for developing web ap

rack 介绍

?

rack是什么?

Rack provides a minimal, modular and adaptable interface for developing web applications in Ruby. By wrapping?HTTPrequests and responses in the simplest way possible, it unifies and distills the?API?for web servers, web frameworks, and software in between (the so-called middleware) into a single method call.

Rack是为开发web应用程序提供了最小的、模块化和适应性强的接口。用最简单的方式封装了HTTPprequest和responses,它统一和提取出了API,用于web服务器、web框架,和软件之间(所谓的中间件)到一个单独的方法call

rack规范?

Rack specification specifies how exactly a Rack application and the web server should communicate :
A Rack application is an Ruby object (not a class) that responds to?call. It takes exactly one argument, theenvironment?and returns an Array of exactly three values: The?status, the?headers, and the?body.

?

简单的说,rack指定了应用程序和web服务器之间如何进行通信,call方法 接受参数environment,然后返回一个数组,包括HTTP状态码(200, 500等),HTTP响应头(Hash),HTTP响应内容(字符串数组)。

?

rack gem?

Rack gem是一个工具的集合,并且是一个使用非常容易的类库,使我们在创建应用程序的时候更容易,它包含了request,response,cookies,sessions,并且引入了很多非常使用的中间件。

?

?

实例1

?

require 'rubygems'require 'rack'class HelloWorld  def call(env)    [200, {"Content-Type" => "text/html"}, ["Hello Rack!"]]  endendRack::Handler::Mongrel.run HelloWorld.new, :Port => 9292
?

?

实例2

?

require 'rubygems'require 'rack'Rack::Handler::Mongrel.run proc {|env| [200, {"Content-Type" => "text/html"}, ["Hello Rack!"]]}, :Port => 9292
?

?

实例3

?

require 'rubygems'require 'rack'def application(env)  [200, {"Content-Type" => "text/html"}, ["Hello Rack!"]]endRack::Handler::Mongrel.run method(:application), :Port => 9292

?

访问:http://localhost:9292/,就可以得到 Hello Rack!

资料:http://m.onkey.org/ruby-on-rack-1-hello-rack

? ? ? ? ?http://guides.rubyonrails.org/rails_on_rack.html

热点排行