Languages
[Edit]
EN

Express.js - wildcard parameters in routing path

10 points
Created by:
Imaan-Morin
1009

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.

 

Simple example

In this section we focus on /path/to/* paht as example.

const express = require('express');                  // npm install express
const createExpression = require('path-to-regexp');  // this package in correct version is automatically attached by Express.js to construct routes expressions

const findWildcard = (expression, path, index = 0) => {
    const matches = expression.exec(path);
    if (matches) {
        return matches[index + 1] ?? '';
    }
    return '';
};

const app = express();

// e.g.
//     ---[URL]----------------------------------------   ---------[wildcard parameter]----------
//                                                        ----[decoded]----    ----[raw]---------
//     http://localhost:3000/path/to/value                value                value
//     http://localhost:3000/path/to/example/resource     example/resource     example/resource
//     http://localhost:3000/path/to/example%2Fresource   example/resource     example%2Fresource
//
const expression = createExpression('/path/to/*');
app.get(['/path/to', '/path/to/*'], (request, response) => {
    const decodedWildcard = request.params[0] ?? '';               // contains decoded wildcard parameter
    const rawWildcard = findWildcard(expression, request.url, 0);  // finds raw not decoded wildcard parameter
    // ...
});

app.listen(3000, () => console.log(`Server is listening on port 3000.`));

 

Complex example

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.

const express = require('express');                  // npm install express
const createExpression = require('path-to-regexp');  // this package in correct version is automatically attached by Express.js to construct routes expressions

const findWildcard = (expression, path, index = 0) => {
    const matches = expression.exec(path);
    if (matches) {
        return matches[index + 1] ?? '';
    }
    return '';
};

const app = express();

// e.g.
//     ---[URL]-----------------------------------------   -------[wildcard parameter]------
//                                                         --[decoded]--    --[raw]---------
//     http://localhost:3000/path/to/a-/b-/etc             '', ''           '', ''
//     http://localhost:3000/path/to/a-1/b-2/etc           '1', '2'         '1', '2'
//     http://localhost:3000/path/to/a-1%202/b-3%204/etc   '1 2', '3 4'     '1%202', '3%204'
//
const expression = createExpression('/path/to/a-*/b-*/etc');
app.get('/path/to/a-*/b-*/etc', (request, response) => {
    const decodedWildcard1 = request.params[0] ?? '';               // contains decoded wildcard parameter
    const decodedWildcard2 = request.params[1] ?? '';               // contains decoded wildcard parameter
    const rawWildcard1 = findWildcard(expression, request.url, 0);  // finds raw not decoded wildcard parameter
    const rawWildcard2 = findWildcard(expression, request.url, 1);  // finds raw not decoded wildcard parameter
    // ...
});

app.listen(3000, () => console.log(`Server is listening on port 3000.`));

 

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join