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

Arun Bohra
  • 495
  • 1
  • 6
  • 16

4 Answers4

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
10

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});
M-Chen-3
  • 2,036
  • 5
  • 13
  • 34
narendra
  • 121
  • 1
  • 6
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