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