Moving to Express 5迁移到Express 5

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.0仍处于测试版发布阶段,但以下是将在该版本中进行的更改预览,以及如何将您的Express 4应用迁移到Express 5。

To install the latest beta and to preview Express 5, enter the following command in your application root directory:要安装最新的测试版并预览Express 5,请在应用程序根目录中输入以下命令:

$ 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.你会立即发现应用程序是否使用了任何不受支持的方法或属性。

Changes in Express 5Express 5的变化

Removed methods and properties删除的方法和属性

Changed改变

Improvements改进

Removed methods and properties删除的方法和属性

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.所以,你需要在升级到版本5后更改你的应用程序。

app.del()

Express 5 no longer supports the app.del() function. Express 5不再支持app.del()函数。If you use this function an error is thrown. 如果使用此函数,将抛出一个错误。For registering HTTP DELETE routes, use the app.delete() function instead.要注册HTTP删除路由,请改用app.delete()函数。

Initially del was used instead of delete, because delete is a reserved keyword in JavaScript. 最初使用del代替delete,因为delete在JavaScript中是一个保留关键字。However, as of ECMAScript 6, delete and other reserved keywords can legally be used as property names.但是,从ECMAScript 6开始,delete和其他保留关键字可以合法地用作属性名。

app.param(fn)

The app.param(fn) signature was used for modifying the behavior of the app.param(name, fn) function. app.param(fn)签名用于修改app.param(name, fn)函数的行为。It has been deprecated since v4.11.0, and Express 5 no longer supports it at all.自v4以来,它一直被弃用。11.0和Express 5不再支持它。

Pluralized method names复数方法名

The following method names have been pluralized. 以下方法名称已被复数化。In Express 4, using the old methods resulted in a deprecation warning. 在Express 4中,使用旧方法会导致一个弃用警告。Express 5 no longer supports them at all:Express 5完全不再支持它们:

req.acceptsCharset() is replaced by req.acceptsCharsets().被替换为req.acceptsCharsets()

req.acceptsEncoding() is replaced by req.acceptsEncodings().被替换为req.acceptsEncodings()

req.acceptsLanguage() is replaced by req.acceptsLanguages().被替换为req.acceptsLanguages()

Leading colon (:) in the name for app.param(name, fn)app.param(name, fn)名称中的前导冒号(:

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. app.param(name, fn)函数名称中的前导冒号(:)是Express 3的一个残余,为了向后兼容,Express 4支持它,并附带了一个弃用通知。Express 5 will silently ignore it and use the name parameter without prefixing it with a colon.Express 5将自动忽略它,并使用name参数,而不在其前面加冒号。

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.如果遵循app.param的Express 4文档,这不会影响代码,因为它没有提到前导冒号。

req.param(name)

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.现在需要在req.paramsreq.bodyreq.query对象中专门查找提交的参数名称。

res.json(obj, status)

Express 5 no longer supports the signature res.json(obj, status). Express 5不再支持签名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.json()方法,如下所示:res.status(status).json(obj)

res.jsonp(obj, status)

Express 5 no longer supports the signature res.jsonp(obj, status). Express 5不再支持签名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.jsonp()方法,如下所示:res.status(status).jsonp(obj)

res.send(body, status)

Express 5 no longer supports the signature res.send(obj, status). Express 5不再支持签名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()方法,如下所示:res.status(status).send(obj)

res.send(status)

Express 5 no longer supports the signature res.send(status), where status is a number. Express 5不再支持签名res.send(status),其中status是一个数字。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.相反,可以使用res.sendStatus(statusCode)函数,该函数设置HTTP响应头状态代码,并发送代码的文本版本:“未找到”、“内部服务器错误”等等。 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.send()函数发送数字,请引用该数字以将其转换为字符串,这样Express就不会将其解释为试图使用不受支持的旧签名。

res.sendfile()

The res.sendfile() function has been replaced by a camel-cased version res.sendFile() in Express 5.res.sendfile()函数已被express5中的一个驼峰大小写版本的res.sendFile()替换。

Changed改变

Path route matching syntax路径匹配语法

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. 路径路径匹配语法是指将字符串作为第一个参数提供给app.all()app.use()app.METHOD()router.all()router.METHOD()router.use()API。The following changes have been made to how the path string is matched to an incoming request:对路径字符串与传入请求的匹配方式进行了以下更改:

Rejected promises handled from middleware and handlers拒绝来自中间件和处理程序的承诺

Request middleware and handlers that return rejected promises are now handled by forwarding the rejected value as an Error to the error handling middleware. 请求中间件和返回被拒绝承诺的处理程序现在通过将被拒绝的值作为Error转发给错误处理中间件来处理。This means that using async functions as middleware and handlers are easier than ever. 这意味着使用async函数作为中间件和处理程序比以往任何时候都更容易。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).当在异步函数中抛出错误或在async函数中await拒绝的承诺时,这些错误将被传递给错误处理程序,就像调用next(err)一样。

Details of how Express handles errors is covered in the error handling documentation.有关Express如何处理错误的详细信息,请参阅错误处理文档

app.router

The app.router object, which was removed in Express 4, has made a comeback in Express 5. app.router对象在Express 4中被删除,但在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.在新版本中,这个对象只是对基本Express路由器的引用,而在Express 3中,应用程序必须显式加载它。

req.host

In Express 4, the req.host function incorrectly stripped off the port number if it was present. 在Express 4中,如果存在端口号,req.host函数会错误地剥离端口号。In Express 5 the port number is maintained.在Express 5中,端口号保持不变。

req.query

The req.query property is no longer a writable property and is instead a getter. req.query属性不再是可写属性,而是getter。The default query parser has been changed from “extended” to “simple”.默认查询解析器已从“扩展”更改为“简单”。

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.此方法现在对所有视图引擎强制执行异步行为,避免了由具有同步实现且违反推荐接口的视图引擎引起的错误。