EN
Express.js - wildcard parameters in routing path
10 points
In this short article, we would like to show how to use wildcard parameters in routing path in Express.js.
Express.js allows to use wildcard parameters in routing paths by inserting star characters (*
). Sometimes it is necessary to get not decoded wildcard parameter values. By default Express.js do not let to get not decoded parameters so some trick related with path-to-regexp
package is needed, what was show as case examples.
The below examples were tested using Express.js v4.18.2.
In this section we focus on /path/to/*
paht as example.
xxxxxxxxxx
1
const express = require('express'); // npm install express
2
const createExpression = require('path-to-regexp'); // this package in correct version is automatically attached by Express.js to construct routes expressions
3
4
const findWildcard = (expression, path, index = 0) => {
5
const matches = expression.exec(path);
6
if (matches) {
7
return matches[index + 1] ?? '';
8
}
9
return '';
10
};
11
12
const app = express();
13
14
// e.g.
15
// ---[URL]---------------------------------------- ---------[wildcard parameter]----------
16
// ----[decoded]---- ----[raw]---------
17
// http://localhost:3000/path/to/value value value
18
// http://localhost:3000/path/to/example/resource example/resource example/resource
19
// http://localhost:3000/path/to/example%2Fresource example/resource example%2Fresource
20
//
21
const expression = createExpression('/path/to/*');
22
app.get(['/path/to', '/path/to/*'], (request, response) => {
23
const decodedWildcard = request.params[0] ?? ''; // contains decoded wildcard parameter
24
const rawWildcard = findWildcard(expression, request.url, 0); // finds raw not decoded wildcard parameter
25
// ...
26
});
27
28
app.listen(3000, () => console.log(`Server is listening on port 3000.`));
In this section we focus on /path/to/a-*/b-*/etc
paht as example.
Motivation: this solution finds application when we want to forward parameters to other logic, e.g. proxy.
xxxxxxxxxx
1
const express = require('express'); // npm install express
2
const createExpression = require('path-to-regexp'); // this package in correct version is automatically attached by Express.js to construct routes expressions
3
4
const findWildcard = (expression, path, index = 0) => {
5
const matches = expression.exec(path);
6
if (matches) {
7
return matches[index + 1] ?? '';
8
}
9
return '';
10
};
11
12
const app = express();
13
14
// e.g.
15
// ---[URL]----------------------------------------- -------[wildcard parameter]------
16
// --[decoded]-- --[raw]---------
17
// http://localhost:3000/path/to/a-/b-/etc '', '' '', ''
18
// http://localhost:3000/path/to/a-1/b-2/etc '1', '2' '1', '2'
19
// http://localhost:3000/path/to/a-1%202/b-3%204/etc '1 2', '3 4' '1%202', '3%204'
20
//
21
const expression = createExpression('/path/to/a-*/b-*/etc');
22
app.get('/path/to/a-*/b-*/etc', (request, response) => {
23
const decodedWildcard1 = request.params[0] ?? ''; // contains decoded wildcard parameter
24
const decodedWildcard2 = request.params[1] ?? ''; // contains decoded wildcard parameter
25
const rawWildcard1 = findWildcard(expression, request.url, 0); // finds raw not decoded wildcard parameter
26
const rawWildcard2 = findWildcard(expression, request.url, 1); // finds raw not decoded wildcard parameter
27
// ...
28
});
29
30
app.listen(3000, () => console.log(`Server is listening on port 3000.`));