mirror of
https://github.com/expressjs/expressjs.com.git
synced 2026-02-21 19:41:33 +00:00
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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user