Category Archives: Puzzles

Puzzling it Out: What is Middleware? What is Rack?

Yes I know You All Know This

As a newbie to development I always feel like there are a zillion things to learn, and I need to learn them all at once. So far, I have focussed on practical coding – Ruby, HTML, Javascript, etc.  I also expanded out to get my head around coding concepts by doing a couple of CS101 courses (Stanford and Udacity).

However, there are some things which get mentioned over and over again, which I don’t understand, and which aren’t explained in any of my ‘beginner’ courses.

For example. Middleware. Rack. I’m not sure exactly where to get this kind of knowledge, but I keep stumbling across little pieces of the puzzle. Here, I’m putting my pieces together, so I don’t forget them, and so they can start painting the picture.

What is Middleware?

Reference: Rails Middleware Walkthrough on Rails Cast #319

This Rails Cast gives a breakdown on the middleware that is in place with normal Rails apps, and even though what each of those pieces of middleware does was pretty much over my head, a few basic concepts came through, which I didn’t understand before watching this:

Middleware is what goes on between receiving a request from a browser and sending the response from your app.

  • The list of the middleware will be different according to the framework you use, as well as your environment (e.g. development vs production)
  • You can see the middleware in a rails app by going $ rake middleware
  • This will give you a list of classes (and unexpectedly to the Rails Casts guy, also an object in one case)
  • A request will run through each of these classes and then at the end finally run your applications routes
  • Pieces of middleware might do things like “set the HTTP request method based on _method parameter” or “capture the remote IP address for later use”

Middleware and Rack

Again, I don’t know exactly why these things are necessary, but I guess they are necessary to make your app run as expected.

Now, having said all that, the next thing to understand is “Rack”. For which I have these pieces of the puzzle:

What is Rack?

  •  “With Rack, you are officially leaving the world of web sites and entering the world of web apps. No longer are you sending static HTML files to the browser; you are programmatically building HTML on-the-fly to send to the browser.” Techiferous
  • “Rack provides a minimal interface between webservers supporting Ruby and Ruby frameworks” 
  • It contains a full stack of Middleware components
  • “Using Rack Middleware you can build applications that directly interact with a HTTP requests environment and can be plugged in Rails, Sinatra, and many other Ruby based Frameworks.” A Rack Introduction
  • Rack is one of those ‘bare minimum components’ that you need for creating modular web applications. A Rails app is actually a collection of Rack and Rails middleware components that work together to form the completed whole.
  • In rails a .ru file is a rackup file

I can’t find one individual resource on understanding these things because I don’t understand a lot of what is said in those casts/blogs. However, I’ve rummaged around and found out enough for now to satisfy my curiosity.

Leave a reply