Skip to content

Powerful path matching

Ravi Teja Gudapati edited this page Jun 4, 2018 · 8 revisions

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.

Variable path segment

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

Regex path segments

Variable path segments can be further extended to match based on Regex patterns. To achieve this, we must,

  1. Provide a name for the variable path segment
  2. 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:

  1. The variable path segment is given the name id in /api/user/:id/setting
  2. The Regex pattern '^[0-9]{1,3}$' is associated to path variable id

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

Glob matching

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!');

What's next?

In the next article, we will learn how to capture the values of the path variables using path parameters.

Basics

Serialization

Forms

Sessions

Authentication

  • Basic authentication
  • Form authentication
  • JSON authentication
  • Authorization
  • OAuth

Database

Security

Real time

  • Server sent events (SSE)
  • Websockets

Deployment

  • systemd
  • Docker
  • AppEngine

API Documentation

Clone this wiki locally