From c1d9a4f64e447109e8489270c4af988f6418ab23 Mon Sep 17 00:00:00 2001 From: Sebastian Beltran Date: Sun, 6 Oct 2024 09:54:39 -0500 Subject: [PATCH] Update migration guide (#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 #1317 * add `brotli` support * adapt router matching syntax --- en/guide/migrating-5.md | 73 ++++++++++++++++++++++++++++++++++------- 1 file changed, 61 insertions(+), 12 deletions(-) diff --git a/en/guide/migrating-5.md b/en/guide/migrating-5.md index 377697d97..969e7417e 100755 --- a/en/guide/migrating-5.md +++ b/en/guide/migrating-5.md @@ -9,9 +9,9 @@ redirect_from: "/guide/migrating-5.html"

Overview

-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. +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. -To install the latest beta and to preview Express 5, enter the following command in your application root directory: +To install this version, you need to have a Node.js version 18 or higher. Then, execute the following command in your application directory: ```console $ 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
  • req.param(name)
  • res.json(obj, status)
  • res.jsonp(obj, status)
  • +
  • res.redirect('back') and res.location('back')
  • +
  • res.redirect(url, status)
  • res.send(body, status)
  • res.send(status)
  • res.sendfile()
  • @@ -41,15 +43,21 @@ You can then run your automated tests to see what fails, and fix problems accord **Improvements**

    Removed methods and properties

    @@ -94,13 +102,22 @@ Express 5 no longer supports the signature `res.json(obj, status)`. Instead, set 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)`. +

    res.redirect(url, status)

    + +Express 5 no longer supports the signature `res.redirect(url, status)`. Instead, use the following signature: `res.redirect(status, url)`. + + +

    res.redirect('back') and res.location('back')

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

    res.send(body, status)

    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)`.

    res.send(status)

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

    res.sendfile()

    @@ -113,15 +130,23 @@ The `res.sendfile()` function has been replaced by a camel-cased version `res.se 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: -- Add new `?`, `*`, and `+` parameter modifiers. -- Matching group expressions are only RegExp syntax. - * `(*)` is no longer valid and must be written as `(.*)`, for example. -- Named matching groups no longer available by position in `req.params`. - * `/:foo(.*)` only captures as `req.params.foo` and not available as `req.params[0]`. -- Regular expressions can only be used in a matching group. - * `/\\d+` is no longer valid and must be written as `/(\\d+)`. -- Special `*` path segment behavior removed. - * `/foo/*/bar` will match a literal `*` as the middle segment. +- The wildcard `*` must have a name, matching the behavior of parameters `:`, use `/*splat` instead of `/*` +- The optional character `?` is no longer supported, use braces instead: `/:file{.:ext}`. +- Regexp characters are not supported. For example: +```js +app.get('/[discussion|page]/:slug', async (req, res) => { + res.status(200).send('ok') +}) +``` +should be changed to: +```js +app.get(['/discussion/:slug', '/page/:slug'], async (req, res) => { + res.status(200).send('ok') +}) +``` + +- Some characters have been reserved to avoid confusion during upgrade (`()[]?+!`), use `\` to escape them. +- Parameter names now support valid JavaScript identifiers, or quoted like `:"this"`.

    Rejected promises handled from middleware and handlers

    @@ -129,10 +154,18 @@ Request middleware and handlers that return rejected promises are now handled by Details of how Express handles errors is covered in the [error handling documentation](/en/guide/error-handling.html). +

    express.urlencoded

    + +The `express.urlencoded` method makes the `extended` option `false` by default. +

    app.router

    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

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

    req.host

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

    res.clearCookie

    + +The `res.clearCookie` method ignores the `maxAge` and `expires` options provided by the user. + +

    res.status

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

    res.vary

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

    Improvements

    res.render()

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

    Brotli encoding support

    + +Express 5 supports Brotli encoding for requests received from clients that support it. \ No newline at end of file