A simple Sinatra example

Here's what you need to do:

  1. gem install sinatra
  2. create an app file (e.g. myapp.rb)
  3. run the app file using ruby
  4. open the browser and visit the local website (e.g. http://127.0.0.1:4567)
  5. observe the results

Example

Here's the contents of myapp.rb:

# myapp.rb
require 'sinatra'

get '/' do
  'Hello world!'
end

get '/*' do
  'Hello world2!' + params.inspect
end

Running the app

I typed ruby myapp.rb

Viewing the local website

visited observed
http://127.0.0.1:4567/ Hello world!
http://127.0.0.1:4567/fun Hello world2!{"splat"=>["fun"], "captures"=>["fun"]}

Command-line output

[2013-08-18 11:11:42] INFO  WEBrick 1.3.1
[2013-08-18 11:11:42] INFO  ruby 1.9.3 (2012-04-20) [i686-linux]
== Sinatra/1.4.3 has taken the stage on 4567 for development with backup from WEBrick
[2013-08-18 11:11:42] INFO  WEBrick::HTTPServer#start: pid=3302 port=4567
127.0.0.1 - - [18/Aug/2013 11:17:45] "GET / HTTP/1.1" 200 12 0.0082
niko.qbitx.com - - [18/Aug/2013:11:17:45 BST] "GET / HTTP/1.1" 200 12
- -> /
127.0.0.1 - - [18/Aug/2013 11:17:45] "GET /favicon.ico HTTP/1.1" 200 68 0.0177
niko.qbitx.com - - [18/Aug/2013:11:17:45 BST] "GET /favicon.ico HTTP/1.1" 200 68
- -> /favicon.ico
127.0.0.1 - - [18/Aug/2013 11:17:45] "GET /favicon.ico HTTP/1.1" 200 68 0.0032
niko.qbitx.com - - [18/Aug/2013:11:17:45 BST] "GET /favicon.ico HTTP/1.1" 200 68
- -> /favicon.ico
127.0.0.1 - - [18/Aug/2013 11:17:49] "GET /fun HTTP/1.1" 200 52 0.0019
niko.qbitx.com - - [18/Aug/2013:11:17:49 BST] "GET /fun HTTP/1.1" 200 52
- -> /fun

Resources

Tags:
Source:
1215hrs.txt
Published:
18-08-2013 12:15