简体   繁体   中英

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

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.

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

Since Express 4.16+ the body parsing functionality has become built into 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.

so you can uninstall body-parser using npm uninstall body-parser , and use the above with express

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM