docs: clarify req.params type and usage with regular expressions (#2101)

Co-authored-by: krzysdz <krzysdz@users.noreply.github.com>
This commit is contained in:
krzysdz
2025-12-14 02:23:16 +00:00
committed by GitHub
parent 2e10ad52a9
commit 0cfa8d07c1
2 changed files with 6 additions and 2 deletions

View File

@@ -8,7 +8,7 @@ console.dir(req.params.name)
// => 'tj'
```
When you use a regular expression for the route definition, capture groups are provided in the array using `req.params[n]`, where `n` is the n<sup>th</sup> capture group. This rule is applied to unnamed wild card matches with string routes such as `/file/*`:
When you use a regular expression for the route definition, capture groups are provided as integer keys using `req.params[n]`, where `n` is the n<sup>th</sup> capture group. This rule is applied to unnamed wild card matches with string routes such as `/file/*`:
```js
// GET /file/javascripts/jquery.js
@@ -16,6 +16,8 @@ console.dir(req.params[0])
// => 'javascripts/jquery.js'
```
Named capturing groups in regular expressions behave like named route parameters. For example the group from `/^\/file\/(?<path>.*)$/` expression is available as `req.params.path`.
If you need to make changes to a key in `req.params`, use the [app.param](/{{ page.lang }}/4x/api.html#app.param) handler. Changes are applicable only to [parameters](/{{ page.lang }}/guide/routing.html#route-parameters) already defined in the route path.
Any changes made to the `req.params` object in a middleware or route handler will be reset.

View File

@@ -8,7 +8,7 @@ console.dir(req.params.name)
// => "tj"
```
When you use a regular expression for the route definition, capture groups are provided in the array using `req.params[n]`, where `n` is the n<sup>th</sup> capture group.
When you use a regular expression for the route definition, capture groups are provided as integer keys using `req.params[n]`, where `n` is the n<sup>th</sup> capture group.
```js
app.use(/^\/file\/(.*)$/, (req, res) => {
@@ -18,6 +18,8 @@ app.use(/^\/file\/(.*)$/, (req, res) => {
})
```
Named capturing groups in regular expressions behave like named route parameters. For example the group from `/^\/file\/(?<path>.*)$/` expression is available as `req.params.path`.
If you need to make changes to a key in `req.params`, use the [app.param](/{{ page.lang }}/5x/api.html#app.param) handler. Changes are applicable only to [parameters](/{{ page.lang }}/guide/routing.html#route-parameters) already defined in the route path.
Any changes made to the `req.params` object in a middleware or route handler will be reset.