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.你会立即发现应用程序是否使用了任何不受支持的方法或属性。
Removed methods and properties删除的方法和属性
app.del()
app.param(name, fn)
的name
参数中的前导冒号req.param(name)
res.json(obj, status)
res.jsonp(obj, status)
res.send(body, status)
resendfile()
Changed改变
Improvements改进
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 Express 5不再支持app.del()
function. app.del()
函数。If you use this function an error is thrown. 如果使用此函数,将抛出一个错误。For registering HTTP DELETE routes, use the 要注册HTTP删除路由,请改用app.delete()
function instead.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, 但是,从ECMAScript 6开始,delete
and other reserved keywords can legally be used as property names.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不再支持它。
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()
。
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.params
、req.body
或req.query
对象中专门查找提交的参数名称。
res.json(obj, status)
Express 5 no longer supports the signature Express 5不再支持签名res.json(obj, status)
. 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 Express 5不再支持签名res.jsonp(obj, status)
. 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 Express 5不再支持签名res.send(obj, status)
. 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 Express 5不再支持签名res.send(status)
, where status
is a number. 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()
替换。
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:对路径字符串与传入请求的匹配方式进行了以下更改:
?
, *
, and +
parameter modifiers.?
、*
和+
参数修饰符。(*)
is no longer valid and must be written as (.*)
, for example.(*)
不再有效,必须写为(.*)
,例如。req.params
.req.params
中的位置不再可用。
/:foo(.*)
req.params.foo
and not available as req.params[0]
.req.params.foo
,不作为req.params[0]
提供。/\\d+
is no longer valid and must be written as /(\\d+)
./\\d+
不再有效,必须写为/(\\d+)
。*
path segment behavior removed.*
路径段行为已删除。
/foo/*/bar
*
as the middle segment.*
作为中间段。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 await
ed 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 在Express 4中,如果存在端口号,req.host
function incorrectly stripped off the port number if it was present. 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”.默认查询解析器已从“扩展”更改为“简单”。
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.此方法现在对所有视图引擎强制执行异步行为,避免了由具有同步实现且违反推荐接口的视图引擎引起的错误。