Routing
Routes match requests by URL pattern and provide a higher-level abstraction over request interception.
Register a Route
unsub := page.Route("**/api/users", func(r *bonk.Route) {
r.Fulfill(200, map[string]string{
"Content-Type": "application/json",
}, `[{"id": 1, "name": "Alice"}]`)
})
defer unsub()
Pattern Matching
Routes use glob patterns:
| Pattern | Matches |
|---|---|
**/api/* |
Any URL containing /api/ followed by one path segment |
**/*.js |
Any URL ending in .js |
https://example.com/** |
Any URL on example.com |
**/users?page=* |
URLs with /users and a page query parameter |
Special characters:
| Character | Meaning |
|---|---|
* |
Match any characters except / |
** |
Match any characters including / |
? |
Match exactly one character |
Route Actions
Fulfill
Respond with custom data:
Continue
Let the request proceed normally:
Abort
Block the request:
Route Properties
Multiple Routes
Multiple routes can be registered. If a URL matches multiple patterns, the first registered route handles it: