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.
Asked
Active
Viewed 1.1k times
18

Arun Bohra
- 495
- 1
- 6
- 16
-
Does this answer your question? [bodyParser is deprecated express 4](https://stackoverflow.com/questions/24330014/bodyparser-is-deprecated-express-4) – pzaenger Mar 09 '21 at 14:00
-
Actually I tried to search for this earlier. I found this answer but it didn't helped me. – Arun Bohra Mar 09 '21 at 19:50
-
See [this answer](https://stackoverflow.com/a/59892173/2436655) – pzaenger Mar 09 '21 at 20:05
4 Answers
34
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.

Happy Patel
- 2,259
- 1
- 16
- 25
9
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.

japrescott
- 4,736
- 3
- 25
- 37

Arun Bohra
- 495
- 1
- 6
- 16
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);
});

Abraham
- 12,140
- 4
- 56
- 92