简体   繁体   English

为什么我收到“bodyParser”已弃用警告?

[英]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.

相关问题 为什么我会收到这个已弃用的警告?! MongoDB - Why am I getting this deprecated warning?! MongoDB 为什么我在React组件渲染中收到此警告“不赞成使用可变样式”? - Why am I get this warning ' Mutating `style` is deprecated' in react component render? 为什么我会收到关于作为 React Child 的函数的警告 - Why am I getting warning for functions as a React Child on this “警告:尝试删除不存在的子项”为什么我在React Native中收到此警告? - “Warning: Trying to remove a child that doesn't exist” Why am I getting this warning in React Native? 为什么我在 for...of 循环中收到“no-unused-vars”警告,我该如何解决? - Why am I getting a 'no-unused-vars' warning in a for...of loop and how do I fix it? 为什么在Travis-CI中收到有关模块未定义的警告? - Why am I getting this warning with Travis-CI about module being undefined? 为什么我收到“构造函数必须在声明时初始化”的Google闭包编译器警告? - Why am I getting “Constructor must be initialized at declaration” google closure compiler warning? 尝试制作地图时,为什么会收到独特的“钥匙”警告? - Why am I getting a unique “key” warning when trying to do a map? 为什么我收到以下 React Native 警告:Can't perform a React state update on an unmounted component - Why am I getting the following React Native warning: Can't perform a React state update on an unmounted component 我为什么会收到此警告:只能更新已安装或正在安装的组件? - Why am I getting this Warning: Can only update a mounted or mounting component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM