กรอบเว็บที่เหมือนซินาตร้าสำหรับ NIM Jester จัดเตรียม DSL สำหรับการสร้างเว็บแอปพลิเคชันอย่างรวดเร็วใน NIM
# example.nim
import htmlgen
import jester
routes:
get " / " :
resp h1 ( " Hello world " )รวบรวมและทำงานด้วย:
cd tests/example
nim c -r example.nim
ดูที่: localhost: 5000
ก่อนที่จะปรับใช้เพื่อการผลิตให้แน่ใจว่าคุณเรียกใช้แอปพลิเคชันของคุณหลังพร็อกซีย้อนกลับ ห้องสมุดนี้ยังไม่แข็งตัวจากการใช้ประโยชน์จาก HTTP Security ดังนั้นแอปพลิเคชันที่เขียนไว้ในนั้นไม่ควรสัมผัสกับอินเทอร์เน็ตสาธารณะ
routes:
get " / " :
# do something here. เส้นทางทั้งหมดจะต้องอยู่ในบล็อก routes
เส้นทางจะถูกดำเนินการตามลำดับที่พวกเขาประกาศ ดังนั้นระวังเมื่อหยุด
เส้นทางเส้นทางอาจมีรูปแบบพิเศษหรือเพียงแค่สตริงคงที่ รูปแบบพิเศษเกือบจะเหมือนกับของซินาตร้าความแตกต่างที่แท้จริงเพียงอย่างเดียวคือการใช้ @ แทน :
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 "รูปแบบใน Jester นั้นมีข้อ จำกัด มากขึ้นเล็กน้อยไม่มีรูปแบบไวด์การ์ด
คุณสามารถใช้ '?' อักขระเพื่อบ่งบอกถึงส่วนเส้นทางที่เป็นทางเลือก
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?" สิ่งนี้มีประโยชน์เพราะตัวตลกจะไม่ตรงกับ "/สวัสดี" หากผู้นำ '/' ไม่ได้เป็นตัวเลือก
Regex ยังสามารถใช้เป็นรูปแบบเส้นทาง การจับภาพ subpattern จะถูกวางใน request.matches เมื่อมีการจับคู่เส้นทาง ตัวอย่างเช่น:
get re " ^ /([0-9]{2}) .html$ " :
resp request.matches[ 0 ] สิ่งนี้จะตรงกับ URL ของแบบฟอร์ม /15.html ในกรณีนี้ request.matches[0] จะเป็น 15
Jester สนับสนุนเงื่อนไขอย่างไรก็ตามพวกเขาถูก จำกัด ไว้ที่เทมเพลต 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
การส่งคืนการตอบกลับจากเส้นทางควรทำโดยใช้หนึ่งในฟังก์ชั่นต่อไปนี้:
respbody headers และ/หรือ status และการโทร returnredirectattachmentอาจมีมากขึ้น ดูเอกสารของ Jester.nim สำหรับข้อมูลเพิ่มเติม
เป็นไปได้ที่จะไม่ใช้แมโคร routes และทำเส้นทางด้วยตัวเอง
คุณสามารถทำได้โดยการเขียนขั้นตอนการ match ของคุณเอง ดูตัวอย่าง 2 สำหรับตัวอย่างเกี่ยวกับวิธีการทำสิ่งนี้
โดยค่าเริ่มต้น Jester ค้นหาไฟล์คงที่ใน ./public public สิ่งนี้สามารถเอาชนะได้โดยใช้ฟังก์ชั่น setStaticDir ไฟล์จะเสิร์ฟเช่นนั้น:
./public/css/style.css -> http://example.com/css/style.css
หมายเหตุ : Jester จะให้บริการเฉพาะไฟล์ที่สามารถอ่านได้โดย others บน Unix/Linux คุณสามารถตรวจสอบสิ่งนี้ด้วย chmod o+r ./public/css/style.css
คุกกี้สามารถตั้งค่าได้โดยใช้ฟังก์ชั่น setCookie
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 เราเตอร์ที่กำหนดเองอนุญาตให้เรียกใช้รหัสการเริ่มต้นของคุณเองและส่งผ่านการตั้งค่าแบบไดนามิกไปยัง Jester ก่อนที่จะเริ่มการวนรอบ Async
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