迁移到 Express 5

¥Moving to Express 5

概述

Express 5.0 仍处于 beta 发布阶段,但这里是发布中将要进行的更改以及如何将 Express 4 应用迁移到 Express 5 的预览。

¥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,请在应用根目录中输入以下命令:

¥To install the latest beta and to preview Express 5, enter the following command in your application root directory:

$ npm install "express@>=5.0.0-beta.1" --save

然后,你可以运行自动化测试以查看失败的原因,并根据下面列出的更新修复问题。解决测试失败后,运行你的应用以查看发生了什么错误。你会立即发现应用是否使用了不受支持的任何方法或属性。

¥You can then run your automated tests to see what fails, and fix problems according to the updates listed below. After addressing test failures, run your app to see what errors occur. You’ll find out right away if the app uses any methods or properties that are not supported.

Express 5 的变化

删除的方法和属性

¥Removed methods and properties

变化

¥Changed

改进

¥Improvements

删除的方法和属性

如果你在应用中使用这些方法或属性中的任何一个,它都会崩溃。因此,你需要在更新到版本 5 后更改你的应用。

¥If you use any of these methods or properties in your app, it will crash. So, you’ll need to change your app after you update to version 5.

app.del()

Express 5 不再支持 app.del() 功能。如果你使用此函数,则会引发错误。要注册 HTTP DELETE 路由,请改用 app.delete() 函数。

¥Express 5 no longer supports the app.del() function. If you use this function an error is thrown. For registering HTTP DELETE routes, use the app.delete() function instead.

最初使用 del 而不是 delete,因为 delete 是 JavaScript 中的保留关键字。但是,从 ECMAScript 6 开始,delete 和其他保留关键字可以合法地用作属性名称。

¥Initially del was used instead of delete, because delete is a reserved keyword in JavaScript. However, as of ECMAScript 6, delete and other reserved keywords can legally be used as property names.

app.param(fn)

app.param(fn) 签名用于修改 app.param(name, fn) 函数的行为。它自 v4.11.0 起已被弃用,Express 5 根本不再支持它。

¥The app.param(fn) signature was used for modifying the behavior of the app.param(name, fn) function. It has been deprecated since v4.11.0, and Express 5 no longer supports it at all.

复数方法名称

以下方法名称已复数。在 Express 4 中,使用旧方法会导致弃用警告。Express 5 根本不再支持它们:

¥The following method names have been pluralized. In Express 4, using the old methods resulted in a deprecation warning. Express 5 no longer supports them at all:

req.acceptsCharset()req.acceptsCharsets() 取代。

¥req.acceptsCharset() is replaced by req.acceptsCharsets().

req.acceptsEncoding()req.acceptsEncodings() 取代。

¥req.acceptsEncoding() is replaced by req.acceptsEncodings().

req.acceptsLanguage()req.acceptsLanguages() 取代。

¥req.acceptsLanguage() is replaced by req.acceptsLanguages().

app.param(name, fn) 名称中的前导冒号 (:)

app.param(name, fn) 函数名称中的前导冒号字符 (:) 是 Express 3 的残余,为了向后兼容,Express 4 通过弃用通知支持它。Express 5 将默默地忽略它并使用 name 参数而不用冒号作为前缀。

¥A leading colon character (:) in the name for the app.param(name, fn) function is a remnant of Express 3, and for the sake of backwards compatibility, Express 4 supported it with a deprecation notice. Express 5 will silently ignore it and use the name parameter without prefixing it with a colon.

如果你遵循 app.param 的 Express 4 文档,这不会影响你的代码,因为它没有提及前导冒号。

¥This should not affect your code if you follow the Express 4 documentation of app.param, as it makes no mention of the leading colon.

req.param(name)

这种检索表单数据的潜在混乱和危险方法已被删除。你现在需要专门在 req.paramsreq.bodyreq.query 对象中查找提交的参数名称。

¥This potentially confusing and dangerous method of retrieving form data has been removed. You will now need to specifically look for the submitted parameter name in the req.params, req.body, or req.query object.

res.json(obj, status)

Express 5 不再支持签名 res.json(obj, status)。相反,设置状态然后将其链接到 res.json() 方法,如下所示:res.status(status).json(obj)

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

res.jsonp(obj, status)

Express 5 不再支持签名 res.jsonp(obj, status)。相反,设置状态然后将其链接到 res.jsonp() 方法,如下所示:res.status(status).jsonp(obj)

¥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.send(body, status)

Express 5 不再支持签名 res.send(obj, status)。相反,设置状态然后将其链接到 res.send() 方法,如下所示:res.status(status).send(obj)

¥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 不再支持签名 res.send(status),其中 status 是数字。相反,使用 res.sendStatus(statusCode) 函数,它设置 HTTP 响应标头状态代码并发送代码的文本版本:”未找到”、”内部服务器错误” 等等。如果你需要使用 res.send() 函数发送数字,请引用该数字以将其转换为字符串,以便 Express 不会将其解释为尝试使用不受支持的旧签名。

¥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()

Express 5 中的 res.sendfile() 功能已被驼峰式版本 res.sendFile() 取代。

¥The res.sendfile() function has been replaced by a camel-cased version res.sendFile() in Express 5.

变化

Path 路由匹配语法

路径路由匹配语法是指将字符串作为第一个参数提供给 app.all()app.use()app.METHOD()router.all()router.METHOD()router.use() API。对路径字符串与传入请求的匹配方式进行了以下更改:

¥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:

从中间件和处理程序处理的被拒绝的 promise

现在,通过将被拒绝的值作为 Error 转发给错误处理中间件来处理返回被拒绝 promise 的请求中间件和处理程序。这意味着使用 async 函数作为中间件和处理程序比以往任何时候都容易。当 async 函数中抛出错误或异步函数内拒绝的 Promise 为 await 时,这些错误将被传递到错误处理程序,就像调用 next(err) 一样。

¥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 awaited inside an async function, those errors will be passed to the error handler as if calling next(err).

Express 如何处理错误的详细信息在 错误处理文档 中进行了介绍。

¥Details of how Express handles errors is covered in the error handling documentation.

app.router

在 Express 4 中删除的 app.router 对象在 Express 5 中卷土重来。在新版本中,此对象只是对基本 Express 路由的引用,这与 Express 3 中的应用必须显式加载它不同。

¥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.host

在 Express 4 中,如果端口号存在,req.host 函数会错误地剥离端口号。在 Express 5 中,端口号保持不变。

¥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.query

req.query 属性不再是可写属性,而是一个 getter。默认查询解析器已从 “extended” 更改为 “simple”。

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