[英]Why am I getting 'bodyParser' is deprecated warning?
I used the latest version of both packages but still, I am getting 'bodyParser' is a deprecated warning.我使用了这两个软件包的最新版本,但我仍然收到“bodyParser”是一个已弃用的警告。 It is not affecting my code but why is this happening?它不会影响我的代码,但为什么会这样?
const express = require("express")
const bodyParser = require("body-parser")
let app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.listen(port, () => {
console.log(`Welcome to ${port}`);
});
It means that body-parser want you to use a different method (.json() or.urlencoded()):这意味着 body-parser 希望您使用不同的方法(.json() 或 .urlencoded()):
https://github.com/expressjs/body-parser/commit/b7420f8dc5c8b17a277c9e50d72bbaf3086a3900 https://github.com/expressjs/body-parser/commit/b7420f8dc5c8b17a277c9e50d72bbaf3086a3900
Same issue occur to my projects also.我的项目也出现同样的问题。 Now in latest express we don't need to import body-parse, we can just use express as follow.现在在最新的 express 中我们不需要导入 body-parse,我们可以使用 express 如下。
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
or if you limit size或者如果你限制大小
app.use(express.urlencoded({ limit: "50mb", extended: false, parameterLimit: 500000000 }));
I think it is better to not use body-parser anymore我认为最好不要再使用 body-parser
Since Express 4.16+ the body parsing functionality has become built into express从 Express 4.16+ 开始,正文解析功能已内置于 express
You can do你可以做
app.use(express.urlencoded({extended: true}));
app.use(express.json()) // To parse the incoming requests with JSON payloads
from directly express, without having to install body-parser. from 直接表达,无需安装 body-parser。
so you can uninstall body-parser using npm uninstall body-parser
, and use the above with express所以你可以使用npm uninstall body-parser
,并使用上面的 express
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.