Route conditions allow you to match requests based on the request path, method, query parameters, cookies, and request headers. You can also use route conditions to match requests based on regular expressions and negation.
As outlined in the Route Criteria section of the CDN-as-Code guide, route criteria are defined as the first argument to the
Router
method being called in the routes.js
file, such as .match()
, .get()
, .post()
, etc.Matching All Requests
Match all requests through either of the following methods:
-
.match
with an empty criteria objectJavaScript1router.match({}, {2 /* ... */3}); -
always
(requires Edgio v7.2.0 or later)JavaScript1router.always({2 /* ... */3});
Simple Path Matching
The syntax for route paths utilizing simple matching is provided by path-to-regexp, which is the same library used by Express.
Named Parameters
Named parameters are defined by prefixing a colon to the parameter name (
:foo
).JavaScript
1router.get('/:foo/:bar', {2 /* ... */3});
Parameter names must use “word characters” (
[A-Za-z0-9_]
).Custom Matching Parameters
Parameters can have a custom regexp, which overrides the default match (
[^/]+
). For example, you can match digits or names in a path:JavaScript
1router.get('/icon-:foo(\\d+).png', {2 /* ... */3});
Tip: Backslashes need to be escaped with another backslash in JavaScript strings.
Custom Prefix and Suffix
Parameters can be wrapped in
{}
to create custom prefixes or suffixes for your segment:JavaScript
1router.get('/:attr1?{-:attr2}?{-:attr3}?', {2 /* ... */3});
Unnamed Parameters
It is possible to write an unnamed parameter that only consists of a regexp. It works the same the named parameter, except it will be numerically indexed:
JavaScript
1router.get('/:foo/(.*)', {2 /* ... */3});
Modifiers
Modifiers must be placed after the parameter (e.g.
/:foo?
, /(test)?
, /:foo(test)?
, or {-:foo(test)}?
).Optional
Parameters can be suffixed with a question mark (
?
) to make the parameter optional.JavaScript
1router.get('/:foo/:bar?', {2 /* ... */3});
Tip: The prefix is also optional, escape the prefix
\/
to make it required.Zero or More
Parameters can be suffixed with an asterisk (
*
) to denote zero or more parameter matches.JavaScript
1router.get('/:foo*', {2 /* ... */3});
The captured parameter value will be provided as an array.
One or More
Parameters can be suffixed with a plus sign (
+
) to denote one or more parameter matches.JavaScript
1router.get('/:foo+', {2 /*... */3});
The captured parameter value will be provided as an array.
Matching Method, Query Parameters, Cookies, and Headers
.match()
can either take a URL path, or an object which allows you to match based on method, query parameters, cookies, or request headers:JavaScript
1router.match(2 {3 path: '/some-path', // value is route-pattern syntax4 method: 'GET', // value is a string5 cookies: {currency: /^(usd)$/i}, // keys are cookie names, values are regular expressions6 headers: {'some-header': /^some-value$/i}, // keys are header names, values are regular expressions7 query: {page: /^(1|2|3)$/}, // keys are query parameter names, values are regular expressions8 },9 {10 /* ... */11 }12);
Negated Route Matching (not)
Previously, we showed how to match requests based on path, method, query parameters, cookies, and request headers. You can also negate these matches by specifying a
not
key in the object passed to your route criteria. For example, the following route matches all requests whose relative path does not match /some-path
:JavaScript
1router.match(2 {3 path: {4 not: '/some-path',5 },6 },7 {8 /* ... */9 }10);
Similarly, you can negate matches based on method, query parameters, cookies, and request headers:
JavaScript
1router.match(2 {3 path: '/some-path',4 query: {5 page: {6 not: /^(1|2|3)$/,7 },8 },9 method: {10 not: 'POST',11 },12 cookies: {13 currency: {14 not: /^(usd)$/i,15 },16 },17 headers: {18 'x-device': {19 not: /^desktop$/i,20 },21 },22 },23 {24 /* ... */25 }26);
This example matches all requests to
/some-path
except for those with query parameter page=1|2|3
Exact Path Matching
As described in Simple Path Matching, this type of route matching is based on path-to-regexp. While this is a rather universal approach to matching requests, Edgio provides additional options for matching requests.
Exact path matching, also known as strict matching, gives you precise control over how requests are matched. Traditionally, you may match
/some-path
with the following route:JavaScript
1router.match('/some-path', {2 /* ... */3});
This will match
/some-path
, /Some-Path
, and other variations in between that are case-insensitive. However, using the exact
function will use strict comparison in matching the request path. The following example shows how to import the exact
function and use it to match requests to /some-path
:JavaScript
1import {Router, exact} from '@edgio/core';23const router = new Router();45router.match(exact('/some-path'), {6 /* ... */7});89export default router;
This matches the path literally, so
/some-path
will match, but /Some-Path
will not.Multiple Path Matching
Multiple path matching uses the
InOperatorValues
type for matching a generic array of values. To use this, you must specify the argument as a RouteCriteria
type for the path
you would like to match against. This type of matching is similar to exact
matching in that it uses strict comparison.For example, the following route matches requests to
/some-path
and /another-path
, but not /Some-Path
or /Another-Path
:JavaScript
1router.match(2 {3 path: ['/some-path', '/another-path'],4 },5 {6 /* ... */7 }8);
Regular Expression Matching
For complex routes that cannot be easily matched using
path-to-regexp
, you can use regular expressions to match requests. For example, the following route matches requests to /some-path
and /another-path
, but not /Some-Path
or /Another-Path
:JavaScript
1router.match(2 {3 path: /^(\/some-path|\/another-path)$/i,4 },5 {6 /* ... */7 }8);
You may also use Negated Route Matching with regular expressions:
JavaScript
1router.match(2 {3 path: {4 not: /^(\/some-path|\/another-path)$/i,5 },6 },7 {8 /* ... */9 }10);
Regular expression matching is also available for matching query parameters, cookies, and request headers, and more. Any property of
RouteCriteria
that accepts CriteriaValue
or OptionalCriteriaValue
types can use a regular expression and negation.To learn about defining conditional logic and nested rules, refer to the Conditional Routes documentation.