1. Computing

Using Rack Middleware

By

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 [
        200,
        {'Content-Type' => 'text/html'},
        ["Hello world!"]
      ]
    end
  end
  
  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.

©2014 About.com. All rights reserved.