Skip to content

Commit c1d9a4f

Browse files
bjohansebasChris Del
authored and
Chris Del
committed
Update migration guide (expressjs#1610)
* add `res.query` and `res.redirect` * add `express.urlencoded` and `req.body` * add `res.clearCookie` and `res.status` * update introduction page * add `res.redirect('back')` and `res.location('back')` * bring changes from expressjs#1317 * add `brotli` support * adapt router matching syntax
1 parent 2627ab3 commit c1d9a4f

File tree

1 file changed

+61
-12
lines changed

1 file changed

+61
-12
lines changed

en/guide/migrating-5.md

Lines changed: 61 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ redirect_from: "/guide/migrating-5.html"
99

1010
<h2 id="overview">Overview</h2>
1111

12-
Express 5.0 is still in the beta release stage, but here is a preview of the changes that will be in the release and how to migrate your Express 4 app to Express 5.
12+
Express 5 is not very different from Express 4; although it maintains the same basic API, there are still changes that break compatibility with the previous version. Therefore, an application built with Express 4 might not work if you update it to use Express 5.
1313

14-
To install the latest beta and to preview Express 5, enter the following command in your application root directory:
14+
To install this version, you need to have a Node.js version 18 or higher. Then, execute the following command in your application directory:
1515

1616
```console
1717
$ npm install "express@>={{ site.data.express.next_version }}" --save
@@ -31,6 +31,8 @@ You can then run your automated tests to see what fails, and fix problems accord
3131
<li><a href="#req.param">req.param(name)</a></li>
3232
<li><a href="#res.json">res.json(obj, status)</a></li>
3333
<li><a href="#res.jsonp">res.jsonp(obj, status)</a></li>
34+
<li><a href="#magic-redirect">res.redirect('back') and res.location('back')</a></li>
35+
<li><a href="#res.redirect">res.redirect(url, status)</a></li>
3436
<li><a href="#res.send.body">res.send(body, status)</a></li>
3537
<li><a href="#res.send.status">res.send(status)</a></li>
3638
<li><a href="#res.sendfile">res.sendfile()</a></li>
@@ -41,15 +43,21 @@ You can then run your automated tests to see what fails, and fix problems accord
4143
<ul class="doclist">
4244
<li><a href="#path-syntax">Path route matching syntax</a></li>
4345
<li><a href="#rejected-promises">Rejected promises handled from middleware and handlers</a></li>
46+
<li><a href="#express.urlencoded">express.urlencoded</a></li>
4447
<li><a href="#app.router">app.router</a></li>
48+
<li><a href="#req.body">req.body</a></li>
4549
<li><a href="#req.host">req.host</a></li>
4650
<li><a href="#req.query">req.query</a></li>
51+
<li><a href="#res.clearCookie">res.clearCookie</a></li>
52+
<li><a href="#res.status">res.status</a></li>
53+
<li><a href="#res.vary">res.vary</a></li>
4754
</ul>
4855

4956
**Improvements**
5057

5158
<ul class="doclist">
5259
<li><a href="#res.render">res.render()</a></li>
60+
<li><a href="#brotli-support">Brotli encoding support</a></li>
5361
</ul>
5462

5563
<h3>Removed methods and properties</h3>
@@ -94,13 +102,22 @@ Express 5 no longer supports the signature `res.json(obj, status)`. Instead, set
94102

95103
Express 5 no longer supports the signature `res.jsonp(obj, status)`. Instead, set the status and then chain it to the `res.jsonp()` method like this: `res.status(status).jsonp(obj)`.
96104

105+
<h4 id="res.redirect">res.redirect(url, status)</h4>
106+
107+
Express 5 no longer supports the signature `res.redirect(url, status)`. Instead, use the following signature: `res.redirect(status, url)`.
108+
109+
110+
<h4 id="magic-redirect">res.redirect('back') and res.location('back')</h4>
111+
112+
Express 5 no longer supports the magic string `back` in the `res.redirect()` and `res.location()` methods. Instead, use the `req.get('Referrer') || '/'` value to redirect back to the previous page. In Express 4, the res.`redirect('back')` and `res.location('back')` methods were deprecated.
113+
97114
<h4 id="res.send.body">res.send(body, status)</h4>
98115

99116
Express 5 no longer supports the signature `res.send(obj, status)`. Instead, set the status and then chain it to the `res.send()` method like this: `res.status(status).send(obj)`.
100117

101118
<h4 id="res.send.status">res.send(status)</h4>
102119

103-
Express 5 no longer supports the signature <code>res.send(<em>status</em>)</code>, where _`status`_ is a number. Instead, use the `res.sendStatus(statusCode)` function, which sets the HTTP response header status code and sends the text version of the code: "Not Found", "Internal Server Error", and so on.
120+
Express 5 no longer supports the signature `res.send(status)`, where `status` is a number. Instead, use the `res.sendStatus(statusCode)` function, which sets the HTTP response header status code and sends the text version of the code: "Not Found", "Internal Server Error", and so on.
104121
If you need to send a number by using the `res.send()` function, quote the number to convert it to a string, so that Express does not interpret it as an attempt to use the unsupported old signature.
105122

106123
<h4 id="res.sendfile">res.sendfile()</h4>
@@ -113,26 +130,42 @@ The `res.sendfile()` function has been replaced by a camel-cased version `res.se
113130

114131
Path route matching syntax is when a string is supplied as the first parameter to the `app.all()`, `app.use()`, `app.METHOD()`, `router.all()`, `router.METHOD()`, and `router.use()` APIs. The following changes have been made to how the path string is matched to an incoming request:
115132

116-
- Add new `?`, `*`, and `+` parameter modifiers.
117-
- Matching group expressions are only RegExp syntax.
118-
* `(*)` is no longer valid and must be written as `(.*)`, for example.
119-
- Named matching groups no longer available by position in `req.params`.
120-
* `/:foo(.*)` only captures as `req.params.foo` and not available as `req.params[0]`.
121-
- Regular expressions can only be used in a matching group.
122-
* `/\\d+` is no longer valid and must be written as `/(\\d+)`.
123-
- Special `*` path segment behavior removed.
124-
* `/foo/*/bar` will match a literal `*` as the middle segment.
133+
- The wildcard `*` must have a name, matching the behavior of parameters `:`, use `/*splat` instead of `/*`
134+
- The optional character `?` is no longer supported, use braces instead: `/:file{.:ext}`.
135+
- Regexp characters are not supported. For example:
136+
```js
137+
app.get('/[discussion|page]/:slug', async (req, res) => {
138+
res.status(200).send('ok')
139+
})
140+
```
141+
should be changed to:
142+
```js
143+
app.get(['/discussion/:slug', '/page/:slug'], async (req, res) => {
144+
res.status(200).send('ok')
145+
})
146+
```
147+
148+
- Some characters have been reserved to avoid confusion during upgrade (`()[]?+!`), use `\` to escape them.
149+
- Parameter names now support valid JavaScript identifiers, or quoted like `:"this"`.
125150

126151
<h4 id="rejected-promises">Rejected promises handled from middleware and handlers</h4>
127152

128153
Request middleware and handlers that return rejected promises are now handled by forwarding the rejected value as an `Error` to the error handling middleware. This means that using `async` functions as middleware and handlers are easier than ever. When an error is thrown in an `async` function or a rejected promise is `await`ed inside an async function, those errors will be passed to the error handler as if calling `next(err)`.
129154

130155
Details of how Express handles errors is covered in the [error handling documentation](/en/guide/error-handling.html).
131156

157+
<h4 id="express.urlencoded">express.urlencoded</h4>
158+
159+
The `express.urlencoded` method makes the `extended` option `false` by default.
160+
132161
<h4 id="app.router">app.router</h4>
133162

134163
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.
135164

165+
<h4 id="req.body">req.body</h4>
166+
167+
The `req.body` property returns `undefined` when the body has not been parsed. In Express 4, it returns `{}` by default.
168+
136169
<h4 id="req.host">req.host</h4>
137170

138171
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.
@@ -141,8 +174,24 @@ In Express 4, the `req.host` function incorrectly stripped off the port number i
141174

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

177+
<h4 id="res.clearCookie">res.clearCookie</h4>
178+
179+
The `res.clearCookie` method ignores the `maxAge` and `expires` options provided by the user.
180+
181+
<h4 id="res.status">res.status</h4>
182+
183+
The `res.status` method only accepts integers in the range of `100` to `999`, following the behavior defined by Node.js, and it returns an error when the status code is not an integer.
184+
185+
<h4 id="res.query">res.vary</h4>
186+
187+
The `res.vary` throws an error when the `field` argument is missing. In Express 4, if the argument was omitted, it gave a warning in the console
188+
144189
<h3>Improvements</h3>
145190

146191
<h4 id="res.render">res.render()</h4>
147192

148193
This method now enforces asynchronous behavior for all view engines, avoiding bugs caused by view engines that had a synchronous implementation and that violated the recommended interface.
194+
195+
<h4 id="brotli-support">Brotli encoding support</h4>
196+
197+
Express 5 supports Brotli encoding for requests received from clients that support it.

0 commit comments

Comments
 (0)