docs: document req.params changes in 5.x (#2092)

Co-authored-by: krzysdz <krzysdz@users.noreply.github.com>
Co-authored-by: Sebastian Beltran <bjohansebas@gmail.com>
This commit is contained in:
krzysdz
2025-12-22 15:07:06 +00:00
committed by GitHub
parent 0f78c3f491
commit 73cf5d63f0
2 changed files with 58 additions and 2 deletions

View File

@@ -1,6 +1,6 @@
<h3 id='req.params'>req.params</h3>
This property is an object containing properties mapped to the [named route "parameters"](/{{ page.lang }}/guide/routing.html#route-parameters). For example, if you have the route `/user/:name`, then the "name" property is available as `req.params.name`. This object defaults to `{}`.
This property is an object containing properties mapped to the [named route "parameters"](/{{ page.lang }}/guide/routing.html#route-parameters). For example, if you have the route `/user/:name`, then the "name" property is available as `req.params.name`. This object defaults to `Object.create(null)` when using string paths, but remains a standard object with a normal prototype when the path is defined with a regular expression.
```js
// GET /user/tj
@@ -8,6 +8,18 @@ console.dir(req.params.name)
// => "tj"
```
Properties corresponding to wildcard parameters are arrays containing separate path segments split on `/`:
```js
app.get('/files/*file', (req, res) => {
console.dir(req.params.file)
// GET /files/note.txt
// => [ 'note.txt' ]
// GET /files/images/image.png
// => [ 'images', 'image.png' ]
})
```
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