Routes
Routes are defined inside a server block using the
get keyword. Each route maps a URL path to a handler.
Defining a route
server 8080 {
get "/hello" {
respond "Hello, world!"
}
}
A route starts with the get keyword, followed by a string
literal for the path, and a block containing the handler body.
Multiple routes
You can define multiple routes in a single server block:
server 8080 {
get "/" {
respond "Home"
}
get "/about" {
respond "About page"
}
get "/health" {
respond "ok"
}
} Path matching
Routes use exact string matching. The request path must match the route path exactly. There are no wildcards, parameters, or pattern matching.
| Route | Request path | Match? |
|---|---|---|
/hello | /hello | Yes |
/hello | /hello/ | No |
/hello | /Hello | No |
/ | / | Yes |
The respond keyword
respond sends an HTTP response from inside a route handler.
It takes a single expression that must evaluate to a string.
get "/greet" {
name = "Harbor"
respond "Hello from " + name
}
The response is returned with a 200 OK status. There is
currently no way to set custom status codes or headers.
Unmatched routes
If a request doesn't match any defined route, the server returns a
404 Not Found response automatically.
HTTP methods
Only GET routes are currently supported. There is no
post, put, delete, or
patch keyword.