diff --git a/_includes/api/en/5x/req-params.md b/_includes/api/en/5x/req-params.md index 720f5ee7f1..4037832cb4 100644 --- a/_includes/api/en/5x/req-params.md +++ b/_includes/api/en/5x/req-params.md @@ -1,6 +1,6 @@

req.params

-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 nth capture group. ```js diff --git a/en/guide/migrating-5.md b/en/guide/migrating-5.md index d16b967929..515856386a 100755 --- a/en/guide/migrating-5.md +++ b/en/guide/migrating-5.md @@ -72,6 +72,7 @@ You can find the list of available codemods [here](https://github.com/expressjs/
  • app.router
  • req.body
  • req.host
  • +
  • req.params
  • req.query
  • res.clearCookie
  • res.status
  • @@ -507,7 +508,7 @@ 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. -

    req.body

    +

    req.body

    The `req.body` property returns `undefined` when the body has not been parsed. In Express 4, it returns `{}` by default. @@ -515,6 +516,49 @@ The `req.body` property returns `undefined` when the body has not been parsed. I 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. +

    req.params

    + +The `req.params` object now has a **null prototype** when using string paths. However, if the path is defined with a regular expression, `req.params` remains a standard object with a normal prototype. Additionally, there are two important behavioral changes: + +**Wildcard parameters are now arrays:** + +Wildcards (e.g., `/*splat`) capture path segments as an array instead of a single string. + +```js +app.get('/*splat', (req, res) => { + // GET /foo/bar + console.dir(req.params) + // => [Object: null prototype] { splat: [ 'foo', 'bar' ] } +}) +``` + +**Unmatched parameters are omitted:** + +In Express 4, unmatched wildcards were empty strings (`''`) and optional `:` parameters (using `?`) had a key with value `undefined`. In Express 5, unmatched parameters are completely omitted from `req.params`. + +```js +// v4: unmatched wildcard is empty string +app.get('/*', (req, res) => { + // GET / + console.dir(req.params) + // => { '0': '' } +}) + +// v4: unmatched optional param is undefined +app.get('/:file.:ext?', (req, res) => { + // GET /image + console.dir(req.params) + // => { file: 'image', ext: undefined } +}) + +// v5: unmatched optional param is omitted +app.get('/:file{.:ext}', (req, res) => { + // GET /image + console.dir(req.params) + // => [Object: null prototype] { file: 'image' } +}) +``` +

    req.query

    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".