Sinatra 初體驗(1) - Route

First Sinatra App

輸入gem install sinatra安裝gem

建立app.rb檔

1
2
3
4
5
6
7
require "sinatra/base"

class App < Sinatra::Base
get '/' do
"Hello World!!"
end
end

建立config.ru

1
2
3
require "./app" # relative file path

run App #class Name

輸入rackup啟動server,在 http://localhost:9292 即可看到Hello World

使用curl來模仿HTTP Verb - GET

1
curl -v "http://localhost:9292"

RESTful HTTP Post:

1
curl -X POST -d "http://localhost:9292"

http-post-and-get-using-curl-in-linux

curl指令用法

改寫app為post

1
2
3
4
5
6
7
require "sinatra/base"

class App < Sinatra::Base
post '/' do
"Hello World!!"
end
end

輸入curl -X Post -v -d "" http://localhost:9292
這邊分號沒有寫錯,因為就是要傳一個空字串給server。
這樣server就會傳回http response

HTTP verb 都來一次

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# app.rb
require "sinatra/base"

class App < Sinatra::Base
get '/' do
"Hello World!!"
end

post '/' do
"Hello World via POST!!"
end

put '/' do
"Hello World via PUT!!"
end

delete '/' do
"Hello World via DELETE!!"
end
end

輸入curl -X POST -v -d "" http://localhost:9292
回應Hello World via POST!!
輸入curl -X PUT -v -d "" http://localhost:9292
回應Hello World via PUT!!
輸入curl -X DELETE -v -d "" http://localhost:9292
回應Hello World via DELETE!!

解析完整的POST

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
> POST / HTTP/1.1
> User-Agent: curl/7.30.0
> Host: localhost:9292
> Accept: */*
> Content-Length: 0
> Content-Type: application/x-www-form-urlencoded
>
< HTTP/1.1 200 OK
< Content-Type: text/html;charset=utf-8
< Content-Length: 22
< X-Xss-Protection: 1; mode=block
< X-Content-Type-Options: nosniff
< X-Frame-Options: SAMEORIGIN
* Server WEBrick/1.3.1 (Ruby/2.0.0/2013-11-22) is not blacklisted
< Server: WEBrick/1.3.1 (Ruby/2.0.0/2013-11-22)
< Date: Fri, 03 Oct 2014 07:58:10 GMT
< Connection: Keep-Alive
<
* Connection #0 to host localhost left intact
Hello World via POST!!%

比較重要的欄位有

  1. 第一行,說明是使用哪種http動詞
  2. Content-Type: text/html;charset=utf-8 說明是html
  3. 最後輸出的就是route相對輸出的內容

接下來

1
2
3
get "/hello/:first_name/:last_name" do |first, last|
"hello#{first}#{last}"
end

在網址列輸入http://0.0.0.0:9292/hello/steven/huang
網頁上就會印出 hellostevenhuang

這樣寫的缺點

這樣寫的話有如果網址列輸入http://0.0.0.0:9292/hello/test
就會出現 Sinatra doesn’t know this ditty 的錯誤,因為這樣的寫法比較沒有彈性,route並沒有match到網址。

修正選擇性欄位的問題

1
2
3
get "/hello/:first_name/?:last_name?" do |first, last|
"hello#{first}#{last}"
end

這樣的話 last_name 就變成了選擇性欄位
有沒有輸入都會進入這個 route
來測試看看:
輸入 http://0.0.0.0:9292/hello/steven
果然輸出了 hellosteven

評論