One File Web Page with Rack

There are times when you need to quickly create small Ruby web
applications. You can do that in many different ways like, creating a
Rails, Sinatra or Rack application.

I like Rack, it gives you more than enough tools to build a web page, and
allows you to have full control of the components that you employ to
build it.

In this article I'll show some examples about how to create simple one page
Rack web applications.

For a bigger overview about Rack look at this article:
Rack Basics – A Rack Introduction.

Also remember, a middleware is nothing more than another Rack
application, and, Rack middlewares can be stacked up in order to compose
bigger systems.

Examples source code

You can download and run the examples by following the next steps:

  git clone git:// gist-935008
  cd gist-935008
  gem install bundler && bundle install

Hello world

Let's start by building a simple hello world example, save the following
code as

Now, run the app with:


Optionally you may specify what port you want your application to run

  rackup -p 8080

And then, go to http://localhost:9292/ or
http://localhost:8080/ (depending on what port
you specified), so you can see your app alive.

Creating an application in this way, requires you to manually specify the
headers, just as I did when I added the Content-Length and
Content-Type headers. Also, this can be accomplished in a simpler way
by using the Rack::ContentLength and Rack::ContentType middlewares:

OK, now let's add ERB rendering to this example.

Now, run the script again. You should see the Hello World message.

In order to show one last example let's write our view using
HAML, and passing variables to the view:

Run the script again. You should see the Hello World message.

That's it, you've seen how to create web pages by simply using
Rack, no frameworks, just Rack.

Thank you for your reading.

Web dev
REST API Calls using SWIFT
React JS: Communication between components
Administrate review