Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion _includes/api/en/5x/req-params.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
<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)`.

```js
// GET /user/tj
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 in the array using `req.params[n]`, where `n` is the n<sup>th</sup> capture group.

```js
Expand Down
19 changes: 18 additions & 1 deletion en/guide/migrating-5.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ You can find the list of available codemods [here](https://github.com/expressjs/
<li><a href="#app.router">app.router</a></li>
<li><a href="#req.body">req.body</a></li>
<li><a href="#req.host">req.host</a></li>
<li><a href="#req.params">req.params</a></li>
<li><a href="#req.query">req.query</a></li>
<li><a href="#res.clearCookie">res.clearCookie</a></li>
<li><a href="#res.status">res.status</a></li>
Expand Down Expand Up @@ -506,14 +507,30 @@ const server = app.listen(8080, '0.0.0.0', (error) => {

The `app.router` object, which was removed in Express 4, has made a comeback in Express 5. In the new version, this object is a just a reference to the base Express router, unlike in Express 3, where an app had to explicitly load it.

<h3 id="req.body">req.body</h3>
<h3 id="req.body">req.body</h3>

The `req.body` property returns `undefined` when the body has not been parsed. In Express 4, it returns `{}` by default.

<h3 id="req.host">req.host</h3>

In Express 4, the `req.host` function incorrectly stripped off the port number if it was present. In Express 5, the port number is maintained.

<h3 id="req.params">req.params</h3>

Parameters specified as wildcards are represented as arrays of segments:

```js
app.get('/*splat', (req, res) => {
// GET /foo/bar
console.dir(req.params)
// => [Object: null prototype] { splat: [ 'foo', 'bar' ] }
})
```

Optional parameters that are not matched do not have a key in `req.params`. In 4.x an unmatched wildcard would be an empty string and a `:` parameter made optional by `?` had a key with value `undefined`.

The object has null prototype.

<h3 id="req.query">req.query</h3>

The `req.query` property is no longer a writable property and is instead a getter. The default query parser has been changed from "extended" to "simple".
Expand Down