1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Using Rack Middleware


2 of 4

A Simple Middleware App

Using middleware applications in a Rackup file is very simple. Simply call the use method with the class name of a middleware application to run. For example, we'll use the ContentLength middleware application with our HelloWorld application, which will add a Content-Length header.

  #\ -p 1337
  use Rack::ContentLength
  class HelloWorld
    def call(env)
      return [
        {'Content-Type' => 'text/html'},
        ["Hello world!"]
  run HelloWorld.new

If you were to examine the HTTP response headers with something like Firebug (a great web development extension for Firefox), you would see that the Rack::ContentLength middleware application added a Content-Length header.

  1. About.com
  2. Technology
  3. Ruby
  4. Networking
  5. Rack
  6. A Simple Middleware App

©2014 About.com. All rights reserved.