From 40197d6831ac2ef209f6ac3aaeca1edade060f66 Mon Sep 17 00:00:00 2001 From: krzysdz Date: Sat, 1 Nov 2025 01:09:59 +0100 Subject: [PATCH 1/2] docs(migrating-5): document req.params changes in 5.x Fixes #2090 --- en/guide/migrating-5.md | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/en/guide/migrating-5.md b/en/guide/migrating-5.md index 99a5b8fb9f..faf0e3e1ca 100755 --- a/en/guide/migrating-5.md +++ b/en/guide/migrating-5.md @@ -71,6 +71,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
  • @@ -506,7 +507,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. @@ -514,6 +515,22 @@ 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

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

    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". From 50de3edb3d995331f68b85b929070f4cf5b168a4 Mon Sep 17 00:00:00 2001 From: krzysdz Date: Sat, 1 Nov 2025 17:32:04 +0100 Subject: [PATCH 2/2] docs(api-5): document wildcard parameters in req.params --- _includes/api/en/5x/req-params.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/_includes/api/en/5x/req-params.md b/_includes/api/en/5x/req-params.md index 2f48c828d4..d68a4f24c5 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)`. ```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 in the array using `req.params[n]`, where `n` is the nth capture group. ```js