expressjs_ExpressJS 5.0的新增功能

expressjs

Express 5.0 is currently in the alpha release stage and it will not be quite different from Express 4. While the underlying API remains the same as that of Express 4, you may need to watch for some deprecated methods that may break your application when you upgrade.

Express 5.0当前处于Alpha发行阶段,与Express 4并没有太大区别。 虽然基础API与Express 4相同,但是您可能需要注意一些不推荐使用的方法,这些方法在升级时可能会破坏您的应用程序

It should, however, be a smooth transition between Express v4 and Express v5 as we will see in the following examples.

但是,这应该是Express v4和Express v5之间的平滑过渡,如以下示例所示。

We've built APIs on Express 4, so we're very excited to see that 5 is coming shortly

我们已经在Express 4上构建了API ,因此我们很高兴看到5即将推出

设置Express 5项目 ( Setting Up An Express 5 Project )

To get started, set up a new node project with npm init and install the latest Express 5 alpha release from npmjs. You can always refer to the release logs from the Express repository's history.md file.

首先,使用npm init设置一个新的节点项目,并从npmjs安装最新的Express 5 alpha版本。 您始终可以从Express资源库的history.md文件中引用发布日志。

$npm install [email protected] --save

We are now ready to see some of the changes. Most of the methods and properties that we will look at here were previously deprecated and have been completely removed in Express 5.

现在,我们准备看到一些更改。 我们将在此处介绍的大多数方法和属性以前均已弃用,并已在Express 5中完全删除。

不推荐使用的方法和属性 ( Deprecated Methods And Properties )

app.del()

app.del()

app.del() has been removed as a HTTP DELETE registration method in favor of app.delete().

app.del()已被移除为HTTP DELETE注册方法,而支持app.delete()

app.del('/resource', (req, res) => res.send('deleted'));
//4: express deprecated app.del: Use app.delete instead
//5: TypeError: app.del is not a function

app.param(fn)

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.

app.param(fn)签名用于修改app.param(name,fn)函数的行为。 从v4.11.0开始不推荐使用它,Express 5不再支持它。

req.param(name)

req.param(名称)

You can no longer access request parameters using the param method. Instead, use the req.params, req.body, or req.query objects.

您不能再使用param方法访问请求参数。 而是使用req.paramsreq.bodyreq.query对象。

// Bad
app.get('/users/:id', (req, res) => {
    res.send(User.find(req.param('id')));
});
//4: express deprecated req.param(name): Use req.params, req.body, or req.query instead
//5: TypeError: req.param is not a function

// Good
app.get('/user/:id', (req, res) => {
    res.send(User.find(req.params.id));
});

复数方法名称 ( Pluralized Method Names )

The following methods have been pluralized in Express 5:

在Express 5中,以下方法已经多元化:

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

    req.acceptsCharset()替换为req.acceptsCharsets()
  • req.acceptsEncoding() is replaced by req.acceptsEncodings().

    req.acceptsEncoding()替换为req.acceptsEncodings()
  • req.acceptsLanguage() is replaced by req.acceptsLanguages().

    req.acceptsLanguage()替换为req.acceptsLanguages()

骆驼式方法 ( Camel-Cased methods )

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

res.sendfile()函数已替换为驼峰式版本res.sendFile()

响应中的状态码链接 ( Status Code Chaining in Responses )

res.json(obj, status)

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

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

res.status(status).json(obj)

res.jsonp(obj, status)

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

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

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:

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

res.status(status).send(obj)

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

If your first experience with the micro framework was Express 4, you probably have no idea what this is all about.

如果您第一次使用微框架是Express 4,那么您可能不知道这是怎么回事。

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(name,fn)函数名称中的冒号(:)前导是Express 3的残余,为了向后兼容,Express 4支持弃用声明。 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文档,这不会影响您的代码,因为它没有提到前导冒号。

让我们谈谈res.send(status) ( Let's Talk about res.send(status) )

In Express 4, res.send(status) where status is a number was deprecated as a way of setting the HTTP status code header in favor of res.sendStatus(statusCode).

在Express 4中,不赞成使用status为数字的res.send(status)来将HTTP状态代码标头设置为res.sendStatus(statusCode)

app.get('/', (req, res) => {
    res.send(500);
});
// 4: express deprecated res.send(status): Use res.sendStatus(status) instead

This is the expected response in express 4 (500 Internal Server Error)

这是快速4中的预期响应(500内部服务器错误)

$ http :8001
HTTP/1.1 500 Internal Server Error
Connection: keep-alive
Content-Length: 21
Content-Type: text/plain; charset=utf-8
Date: Wed, 08 Feb 2017 12:01:48 GMT
ETag: W/"15-3JQVFLwoG6yepWGqlDPA/A"
X-Powered-By: Express

Internal Server Error

The same code would return the exact opposite in express 5 (200 status code with '500' message).

相同的代码将返回快递5中的完全相反的内容(带有“ 500”消息的200状态代码)。

$ http :8000
HTTP/1.1 200 OK
Connection: keep-alive
Content-Length: 3
Content-Type: application/json; charset=utf-8
Date: Wed, 08 Feb 2017 12:04:22 GMT
ETag: W/"3-zuYxEhwuySMvOi8CitXImw"
X-Powered-By: Express

500

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不再支持签名res.send(status),其中status是数字。 而是使用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不会将其解释为尝试使用不受支持的旧签名。

变更与改进 ( Changes And Improvements )

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.

在Express 4中已删除的app.router对象在Express 5中已卷土重来。在新版本中,此对象只是对基本Express路由器的引用,而在Express 3中,应用程序必须显式地加载它。

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.

在Express 4中,req.host函数错误地剥离了端口号(如果存在)。 在Express 5中,将保留端口号。

app.get('/', (req, res) => {
   res.status(200).send(req.host);
});
// 4: localhost
// 5: localhost:8000

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.

从Express 4.7和Express 5开始,如果要使用自己的函数进行查询字符串解析逻辑,则查询解析器选项可以接受false来禁用查询字符串解析。

res.render()

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.

现在,此方法可对所有视图引擎强制执行异步行为,从而避免了由具有同步实现且违反建议接口的视图引擎引起的错误。

Read more about this in this Express Github issue.

阅读本期Express Github中的更多内容。

近期的拟议变更 ( Proposed Changes in the Near Future )

Middleware Promises

中间件承诺

Promises have become quite popular in asynchronous development and it's been proposed that next() returns a promise so that middleware can be resolved and propagated through the next method.

在异步开发中,承诺已经变得非常流行,并且有人提出 next()返回一个promise,以便可以通过next方法解析和传播中间件。

This is how that would look like.

这就是它的样子。

app.use(function (req, res, next) {
  // a promise must be returned, 
  // otherwise the function will be assumed to be synchronous
  return User.get(req.session.userid).then(function (user) {
    req.user = user
  })
  .then(next) // execute all downstream middleware
  .then(function () {
    // send a response after all downstream middleware have executed
    // this is equivalent to koa's "upstream"
    res.send(user)
  })
})

结论 ( Conclusion )

Express 5 is still in alpha so there are bound to be changes. I will keep updating this post as the updates are rolled out. Take a look at this pull request if you want to see an updated list of changes for release.

Express 5仍处于Alpha状态,因此肯定会有变化。 随着更新的推出,我将继续更新此帖子。 如果要查看要发布的更新更改列表,请查看此请求 。

翻译自: https://scotch.io/tutorials/whats-new-in-expressjs-5-0

expressjs

你可能感兴趣的:(中间件,java,python,javascript,vue,ViewUI)