Nodejs Express 是过时了还是怎么, 老是给提示?
Nodejs Express 是过时了还是怎么, 老是给提示?
我在代码里用了一下 bodyParser
:
app.use express.bodyParser()
结果马上给我来一堆警告:
connect.multipart() will be removed in connect 3.0
visit https://github.com/senchalabs/connect/wiki/Connect-3.0 for alternatives
connect.limit() will be removed in connect 3.0
前几天就觉得 Express 有点异样, 官网不知道是不是否过时了
http://expressjs.com/api.html#req.body
This property is an object containing the parsed request body. This feature is provided by the bodyParser() middleware
Koa 要来也不至于这边先跟不上吧, Express 团队最近是有什么状况么?
标题:Node.js Express 是否已经过时?为什么总是出现提示?
内容:
最近在使用 Node.js 的 Express 框架时遇到了一些问题。我尝试使用 bodyParser
中间件来处理请求体,但遇到了一系列警告信息。
app.use(express bodyParser());
运行后,出现了以下警告:
connect.multipart() will be removed in connect 3.0
visit https://github.com/senchalabs/connect/wiki/Connect-3.0-for-alternatives
connect.limit() will be removed in connect 3.0
这让我感到困惑,因为前几天我还觉得 Express 似乎有些变化。我查看了 Express 的官方文档,发现它仍然存在,但似乎有一些问题:
“This property is an object containing the parsed request body. This feature is provided by the bodyParser() middleware.”
我注意到 Koa 即将发布新版本,但这不应该影响 Express 的功能。请问 Express 团队是否有任何变动或计划?
解答
上述警告是因为 bodyParser
已经被废弃,并且不再包含在 Express 的默认中间件中。你可以通过安装第三方库 body-parser
来解决这个问题。
首先,你需要安装 body-parser
:
npm install body-parser
然后,在你的应用中使用 body-parser
替代 express.bodyParser()
:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
// 使用 body-parser 处理请求体
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// 示例路由
app.post('/api/example', (req, res) => {
console.log(req.body);
res.send('Received data');
});
app.listen(3000, () => {
console.log('Server running on port 3000');
});
在这个例子中,我们使用了 body-parser
提供的两个方法:urlencoded
和 json
。urlencoded
用于解析 URL 编码的数据,而 json
用于解析 JSON 数据。这样可以确保你的应用能够正确地处理不同格式的请求体数据。
此外,如果你希望避免这些警告信息,建议你更新到最新的 Express 版本,以确保你使用的是最稳定和安全的版本。
connect 发布了 3.0, 移除了所有中间件。 express 发布了 4.0,移除了 connect 的依赖。 express 老版本引用了 connect 2.x,所以会有这个提示。
defunctzombie 这个哥们现在是 express 的主要维护者,jonathanong 也会维护。TJ 基本不再管 express 了,和 jonathanong 投入到 koa 了。
奇怪有了 4.0 可 npm 上还是放 3.5 , 卖的什么药啊… https://www.npmjs.org/package/express 那现在用的 Express 怎么跟进版本好(还有文档…)?
//app.use(express.bodyParser()); connect 3.0之后改成下面两个
app.use(express.json());
app.use(express.urlencoded());
"3.4.8": "2014-01-14T04:51:15.079Z",
"4.0.0-rc1": "2014-03-02T16:19:53.255Z",
"4.0.0-rc2": "2014-03-05T06:34:13.334Z",
"3.5.0": "2014-03-06T22:58:36.227Z",
"4.0.0-rc3": "2014-03-12T01:39:53.076Z",
"4.0.0-rc4": "2014-03-25T02:54:51.021Z",
"3.5.1": "2014-03-25T20:59:05.986Z
4.0.0 的 rc 版本已经发布了。但是由于3.x 和 4.x 相差有点大,还是在往回修 3.x 的bug,估计再过段时间就不会维护3.x了
现在要用 express 的话,直接上 4.x 吧
关于文档。。 express 这种规模的库都是直接看代码的,还能够学习一些编码风格和用法。
原来是 npm info
里看的.
好吧, 看代码靠谱. 如果马上发布的话总觉得文档也很快要跟上.
express 4.0 和connect 3.0 都在等 node 0.12 发布, 这个很快了, 建议大家直接上新的版本哈
话说 0.12
是什么时候发啊?
直接用koa吧
为什么要等node发布新版?
我遇到了和楼主一样的问题,虽然可以解决那个警告问题,但是我在做一个文件上传的时候,需要用到req.files这个属性,如果不用app.use(express.bodyParser())这个代码就会报错了,不知道有没有人有解决方法
Express 并没有过时。这些警告信息是因为 bodyParser
已经被移出 Express 核心库,并推荐使用独立的中间件包。你可以使用 express.json()
和 express.urlencoded()
来替代 bodyParser
。
示例代码
const express = require('express');
const app = express();
// 使用内置的 JSON 和 URL-encoded 解析中间件
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.post('/example', (req, res) => {
console.log(req.body); // 请求体将被解析为对象
res.send('Data received');
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
解释
express.json()
: 用于解析 JSON 格式的请求体。express.urlencoded({ extended: true })
: 用于解析 URL 编码的请求体(通常是表单提交)。
过去的 bodyParser
中间件
在 Express 4.x 版本之前,bodyParser
是一个集成了多种功能的中间件,例如处理 JSON 和 URL 编码的请求体。从 Express 4.x 开始,bodyParser
被分离成独立的中间件包,以减少依赖并提高灵活性。
通过使用 express.json()
和 express.urlencoded()
,你可以确保你的应用与最新的 Express 版本保持兼容,并且获得更好的性能和稳定性。