Kerangka kerja web seperti Sinatra untuk NIM. Jester menyediakan DSL untuk membuat aplikasi web dengan cepat di NIM.
# example.nim
import htmlgen
import jester
routes:
get " / " :
resp h1 ( " Hello world " )Kompilasi dan jalankan dengan:
cd tests/example
nim c -r example.nim
Lihat di: Localhost: 5000
Sebelum menggunakan produksi memastikan Anda menjalankan aplikasi Anda di belakang proxy terbalik. Perpustakaan ini belum dikeraskan dengan eksploitasi keamanan HTTP sehingga aplikasi yang ditulis di dalamnya tidak boleh terpapar ke internet publik.
routes:
get " / " :
# do something here. Semua rute harus berada di dalam blok routes .
Rute akan dieksekusi dalam urutan bahwa mereka dinyatakan. Jadi berhati -hatilah saat berhenti.
Jalur rute mungkin berisi pola khusus atau hanya string statis. Pola khusus hampir identik dengan Sinatra, satu -satunya perbedaan nyata adalah penggunaan @ bukannya : .
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 "Pola -pola di badut saat ini sedikit lebih terbatas, tidak ada pola wildcard.
Anda dapat menggunakan '?' karakter untuk menandakan bagian jalur opsional.
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 "Dalam hal ini Anda mungkin ingin membuat '/' opsional terkemuka juga, Anda dapat melakukan ini dengan mengubah polanya menjadi "/halo/?@Name?". Ini berguna karena Jester tidak akan cocok dengan "/halo" jika terkemuka '/' tidak dibuat opsional.
Regex juga dapat digunakan sebagai pola rute. Subpattern capture akan ditempatkan dalam request.matches saat rute dicocokkan. Misalnya:
get re " ^ /([0-9]{2}) .html$ " :
resp request.matches[ 0 ] Ini akan cocok dengan URL Formulir /15.html . Dalam hal ini request.matches[0] akan menjadi 15 .
Jester mendukung kondisi, namun mereka terbatas pada template cond sederhana.
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. " Semua badan rute memiliki objek request implisit. Objek ini didokumentasikan dalam Jester.NIM dan dokumentasi dapat dihasilkan dengan mengeksekusi nim doc jester.nim .
Mengembalikan respons dari rute harus dilakukan dengan menggunakan salah satu fungsi berikut:
resp .body , headers dan/atau status dan panggilan return .redirectattachmentMungkin ada lebih banyak. Lihatlah dokumentasi Jester.NIM untuk info lebih lanjut.
Dimungkinkan untuk tidak menggunakan routes makro dan melakukan routing sendiri.
Anda dapat melakukan ini dengan menulis prosedur match Anda sendiri. Lihatlah Contoh2 untuk contoh tentang cara melakukan ini.
Secara default Jester mencari file statis di ./public . Ini dapat ditimpa menggunakan fungsi setStaticDir . File akan disajikan seperti itu:
./public/css/style.css -> http://example.com/css/style.css
Catatan : Jester hanya akan melayani file, yang dapat dibaca oleh others . Di Unix/Linux Anda dapat memastikan ini dengan chmod o+r ./public/css/style.css .
Cookie dapat diatur menggunakan fungsi setCookie .
get " / " :
# Set a cookie "test:value" and make it expire in 5 days.
setCookie ( " test " , @ " value " , daysForward ( 5 )) Mereka kemudian dapat diakses menggunakan Table[string, string] request.cookies .
Objek permintaan menyimpan semua informasi tentang permintaan saat ini. Anda dapat mengaksesnya dari rute menggunakan variabel request . Itu didefinisikan sebagai:
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 Router khusus memungkinkan menjalankan kode inisialisasi Anda sendiri dan meneruskan pengaturan dinamis ke Jester sebelum memulai loop 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 ()Kode untuk ini sangat mirip dengan kode untuk sinatra yang diberikan di sini: http://help.github.com/post-receive-hooks/
import jester, json
routes:
post " / " :
var push = parseJson ( @ " payload " )
resp " I got some JSON: " & $ push