-
Notifications
You must be signed in to change notification settings - Fork 35
Powerful path matching
Besides exact path matching we saw in an earlier article, Jaguar also supports advanced techniques to invoke route handlers based on regular expression matching and globs. We will explore these techniques in this article.
Starting a path segment with character :
informs Jaguar that this segment should match any value.
server.get('/api/user/:/setting', (ctx) => 'Invoked path ${ctx.uri.path}!');
For example, the route handler above will be invoked for the following request paths:
/api/user/1/setting
/api/user/123/setting
/api/user/abc/setting
Variable path segments can be further extended to match based on Regex patterns. To achieve this, we must,
- Provide a name for the variable path segment
- Associate a Regex pattern to it using
pathRegEx
parameter
server.get('/api/user/:id/setting',
(ctx) => 'Settings requested for user ${ctx.pathParams['id']}!',
pathRegEx: {'id': r'^[0-9]{1,3}$'});
Notice how:
- The variable path segment is given the name
id
in /api/user/:id/setting - The Regex pattern
'^[0-9]{1,3}$'
is associated to path variableid
The route handler above will be invoked for the following request paths:
/api/user/1/setting
/api/user/123/setting
And not for the following request paths:
/api/user/abc/setting
The glob path segment (*
) can be used as the last path segment to match all the following path segments.
server.get('/api/v1/*', (_) => 'v1 API deprecated!');
In the next article, we will learn how to capture the values of the path variables using path parameters.
Basics
- Route handler
- Path matching
- Path parameters
- Query parameters
- Serving static files
- Cookies
- Controller
- Parameter binding
- Hot reload
Serialization
Forms
Sessions
Authentication
- Basic authentication
- Form authentication
- JSON authentication
- Authorization
- OAuth
- MongoDb
- PostgreSQL
- MySQL
- Establish connection
- ORM
- Server sent events (SSE)
- Websockets
- systemd
- Docker
- AppEngine