Body Parser Deprecated
Asked Answered
C

4

18

I am currently learning node and express. But when I require and use body-parser my code editor(VS Code) says that it is deprecated. How do I work with this? I will link the image of my editor.enter image description here

Cycloid answered 9/3, 2021 at 13:58 Comment(3)
Does this answer your question? bodyParser is deprecated express 4Insignificance
Actually I tried to search for this earlier. I found this answer but it didn't helped me.Cycloid
See this answerInsignificance
C
10

Body parser is now added to Express. You can use it like the following:

app.use(express.json());

You can add this middleware to the code which will then be able to use json methods.

Cycloid answered 12/7, 2021 at 14:1 Comment(0)
S
35

If you facing 'bodyParser' is deprecated.

Just do

app.use(express.urlencoded({extended: true})); 
app.use(express.json());

Note: If you are using 4.16.0 or later express version.

Sogdiana answered 22/5, 2021 at 13:33 Comment(0)
N
11

body parser package is deprecated. If you are using latest version of express you don't have to install body-parser package.

You can directly use

app.use(express.urlencoded({extended:true});
Noma answered 13/4, 2021 at 14:25 Comment(0)
C
10

Body parser is now added to Express. You can use it like the following:

app.use(express.json());

You can add this middleware to the code which will then be able to use json methods.

Cycloid answered 12/7, 2021 at 14:1 Comment(0)
P
0

Don't use body-parser

Body parsing is now builtin with express So, simply do

app.use(express.json()) //For JSON requests
app.use(express.urlencoded({extended: true}));

directly from express

You can uninstall body-parser using npm uninstall body-parser



Then you can get the POST content of the request using req.body

app.post("/yourpath", (req, res)=>{

    var postData = req.body;

    //Or like this, for string JSON body

    var postData = JSON.parse(req.body);
});
Pompea answered 8/12, 2021 at 20:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.