Hello World app in Sinatra - Introduction to Rack-Based Apps in Ruby
I’ve been rediscovering Ruby (just Ruby, not Ruby on Rails), playing with rack-based apps and Mongoid, a object-document-mapper for MongoDB. I strongly believe Ruby and MongoDB are a great pair, kind of similar and better than the classic PHP and MySQL duo.
So, this is the first series of posts that show you how to use Sinatra, a rack-based framework to write web applications in a breeze.
After you have both, open a command line and type in the following (works on Windows, Linux and OSX):
gem install sinatra
That’s pretty much it the only requirement for now. Later we will add some more gems.
'Hello World' in Sinatra
So, what comes next? That’s right, the ‘hello world’ application that is basic in almost any language.
Write a file named sinatra.rb with the following:
require 'sinatra' get '/' do "Hello World" end
Save the file.
Now in your command line, browse to the folder where sinatra.rb is, and type in the following:
That will start your fresh web application on port 4567 by default. Open your favorite web browser and go to http://0.0.0.0:4567. You will see the Hello world message.
There you go, you wrote your first Sinatra application in less than 2 minutes (according to my calculations).
As you can see, this is just the beginning. Sinatra is a really powerful tool and I encourage you to read the documentation to learn more about this rack-based solution.