start web server using given routes
Parameters
serve routes :function :block
Attributes
| port |
:integer |
use given port. Default: 18966 |
| silent |
|
don't print info log |
| chrome |
|
open in Chrome windows as an app |
Returns
Examples
copy
serve .port: 9000 [
; simple static routes
GET "/" -> "Welcome to my site!"
GET "/about" -> "About us"
; regex route with a named capture group
GET {//user/(?[a-z]+)/} $[name][
emit.html ~"Hello, |name|!"
]
; POST with JSON body
POST "/login" $[username, password][
emit.json write.json #[
msg: ~"Welcome back, |username|!"
] ø
]
GET {//.*/} -> "catch-all fallback"
]
; $> curl localhost:9000/
; Welcome to my site!%
; $> curl localhost:9000/about
; About us%
; $> curl localhost:9000/user/john
; Hello, john!%
; $> curl -X POST localhost:9000/login -d "username=admin&password=secret"
; {
; "msg": "Welcome back, admin!"
; }%
; $> curl localhost:9000/something-else
; catch-all fallback%
copy
serve $[req][
inspect req
;[ :dictionary
; method : GET :string
; path : / :string
; uri : / :string
; body : :string
; query : [ :dictionary
; ]
; headers : [ :dictionary
; ...
; ]
; we have to return either a string
; or a dictionary like:
#[
body: "..."
status: 200
headers: #[
;...
]
]
]
Related