迁移到Express 5

概览

Express5.0仍然处于alpha发布阶段, Express 5.0仍然处于alpha发布阶段,这个文档告诉你以及如何将Express 4应用程序迁移到Express 5中。.

Express 5与Express 4没有太大区别:API的更改并不像3.0到4.0那样显著。尽管基本API保持不变,但仍有一些变化;换句话说,如果您将其更新为Express 5,则现有的Express 4程序可能无法工作.

要安装最新的alpha和预览Express 5,请在应用程序根目录中输入以下命令:

$ npm install express@5.0.0-alpha.2 --save

然后,您可以运行您的自动化测试来查看失败,并根据下面列出的更新修复问题。在处理测试失败后,运行你的应用程序,看看会发生什么错误。如果应用程序使用不支持的任何方法或属性,您将立即找到答案。

Express 5 的变化

下面是更改列表 (as of the alpha 2 release ) 这将会影响你如何使用express. See the pull request for a list of all the planned features.

移除的方法和属性(Removed methods and properties)

修改(Changed)

改进(Improvements)

移除的方法和属性(Removed methods and properties)

如果你在你的应用程序中使用这些方法或属性,它会崩溃。所以,你需要在更新到版本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 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.

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)

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.

Pluralized method names

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() is replaced by req.acceptsCharsets().

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

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

Leading colon (:) in the name for 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. Express 5 will silently ignore it and use the name parameter without prefixing it with a colon.

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)

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

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

修改(Changed)

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

req.query

In Express 4.7 and Express 5 onwards, the query parser option can accept false to disable query string parsing when you want to use your own function for query string parsing logic.

改进(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.