---
title: "rails中自定义rack"
author: "rshua"
date: "2011-04-11T16:22:54Z"
tags: ["HTML", "Rack", "Rails"]
categories: ["编程语言"]
slug: "rails-rack"
draft: false
lastmod: "2011-04-11T16:22:54Z"
id: 997990
url: "https://rshua.iteye.com/blog/997990"
---

具体rack是啥，middleware是啥，metal是啥就不说了！

看看自定义rack吧：

在lib下面定义一个这样的文件：内容如下：

```
class ModelAccessArea
  def initialize(app)
    @app = app
  end
  
  def call(env)
    if env["PATH_INFO"] =~ /^\/poller/
      [200, {"Content-Type" => "text/html"}, "Hello, World"]
    else
      [404, {"Content-Type" => "text/html"}, "Not Found"]
    end
  end
end
```

 在initializers中定义一个文件：内容如下：

```
YourAppName::Application.config.middleware.use ::ModelAccessArea
```

 这样一来的话：

  http://localhost:3000/poller  
  显示 Hello, World

其他请求均为not found!