NIM的類似Sinatra的網絡框架。 Jester提供了一個DSL,用於快速在NIM中創建Web應用程序。
# example.nim
import htmlgen
import jester
routes:
get " / " :
resp h1 ( " Hello world " )編譯並運行:
cd tests/example
nim c -r example.nim
查看:Localhost:5000
在部署到生產之前,請確保您在反向代理後面運行應用程序。該圖書館尚未對HTTP安全性利用進行硬化,因此不應將其撰寫的應用程序暴露於公共互聯網上。
routes:
get " / " :
# do something here.所有路線必須在routes塊中。
路線將按照聲明的順序執行。因此,停止時要小心。
路徑路徑可能包含特殊模式或僅靜態字符串。特殊模式幾乎與Sinatra的模式相同,唯一真正的區別是使用@而不是: 。
get " /hello/@name " :
# This matches "/hello/fred" and "/hello/bob".
# In the route ``@"name"`` will be either "fred" or "bob".
# This can of course match any value which does not contain '/'.
resp " Hello " & @ " name "小丑的模式目前有些有限,沒有通配符的模式。
您可以使用“?”字符表示可選路徑零件。
get " /hello/@name? " :
# This will match what the previous code example matches but will also match
# "/hello/".
if @ " name " == " " :
resp " No name received :( "
else :
resp " Hello " & @ " name "在這種情況下,您可能也需要使領先的'/'可選,可以通過將模式更改為“/hello/?@name?”來做到這一點。這很有用,因為如果未使領先的'/'可選,小丑將不匹配“/hello”。
Regex也可以用作路線模式。副本捕獲將被放置在request.matches中。匹配路由時匹配。例如:
get re " ^ /([0-9]{2}) .html$ " :
resp request.matches[ 0 ]這將匹配表單/15.html的URL。在這種情況下request.matches[0]為15 。
小丑支持條件,但是它們僅限於簡單的cond模板。
routes:
get " /@name " :
cond @ " name " == " daniel "
# ``cond`` will pass execution to the next matching route if @"name" is not
# "daniel".
resp " Correct, my name is daniel. "
get " /@name " :
# This will be the next route that is matched.
resp " No, that's not my name. " 路由物體都有一個隱式request對象。該對象記錄在jester.nim中,可以通過執行nim doc jester.nim來生成文檔。
從路線返迴響應應使用以下功能之一進行:
resp功能之一。body , headers和/或status並調用return 。redirect函數attachment功能可能還有更多。查看jester.nim的文檔以獲取更多信息。
可能不使用宏觀routes並自己進行路由。
您可以通過編寫自己的match程序來做到這一點。查看示例2以獲取有關如何執行此操作的示例。
默認情況下,Jester在./public中查找靜態文件。這可以使用setStaticDir函數過度。文件將像這樣:
./public/css/style.css- -> http://example.com/css/style.css
注意:小丑只能提供others可以讀取的文件。在UNIX/Linux上,您可以使用chmod o+r ./public/css/style.css確保此功能。
可以使用setCookie函數設置cookie。
get " / " :
# Set a cookie "test:value" and make it expire in 5 days.
setCookie ( " test " , @ " value " , daysForward ( 5 ))然後可以使用request.cookies過程訪問它們,該過程返回Table[string, string] 。
請求對象保留有關當前請求的所有信息。您可以使用request變量從路由訪問它。它被定義為:
Request * = ref object
params * : StringTableRef # # Parameters from the pattern, but also the
# # query string.
matches * : array [ MaxSubpatterns , string ] # # Matches if this is a regex
# # pattern.
body * : string # # Body of the request, only for POST.
# # You're probably looking for ``formData``
# # instead.
headers * : StringTableRef # # Headers received with the request.
# # Retrieving these is case insensitive.
formData * : MultiData # # Form data; only present for
# # multipart/form-data
port * : int
host * : string
appName * : string # # This is set by the user in ``run``, it is
# # overriden by the "SCRIPT_NAME" scgi
# # parameter.
pathInfo * : string # # This is ``.path`` without ``.appName``.
secure * : bool
path * : string # # Path of request.
query * : string # # Query string of request.
cookies * : StringTableRef # # Cookies from the browser.
ip * : string # # IP address of the requesting client.
reqMeth * : HttpMethod # # Request method, eg. HttpGet, HttpPost
settings * : Settings 自定義路由器允許在啟動異步循環之前運行自己的初始化代碼,並將動態設置傳遞給小丑。
import asyncdispatch, jester, os, strutils
router myrouter:
get " / " :
resp " It's alive! "
proc main () =
let port = paramStr ( 1 ). parseInt (). Port
let settings = newSettings (port = port)
var jester = initJester (myrouter, settings = settings)
jester. serve ()
when isMainModule :
main ()此的代碼與此處給出的Sinatra的代碼非常相似:http://help.github.com/post-receive-hooks/
import jester, json
routes:
post " / " :
var push = parseJson ( @ " payload " )
resp " I got some JSON: " & $ push