ExpressJS Error: Body-Parser Deprecated

Body parser is deprecated.How can I correct the bodyParser syntax? Here is my code.

const express = require('express');

const bodyParser = require('body-parser');

const MongoClient = require('mongodb').MongoClient;

const password ='sirus4213';

const uri = "mongodb+srv://naturalUser:sirus4213@cluster0.hvcun.mongodb.net/naturaldb?retryWrites=true&w=majority";

const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });

const app = express();

app.use(bodyParser.json());

app.use(bodyParser.urlencoded({ extended: false }));

app.get('/', (req,res) =>{

    res.sendFile(__dirname + '/index.html');

})

client.connect(err => {

  const productCollection = client.db("naturaldb").collection("products");

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

    const product = req.body;

    productCollection.insertOne(product)

   .then(result =>{

     console.log('data added successfully');

     res.send('success');
   })
  })
});

app.listen(3000);

Without seeing your dependencies in your package.json, I can only guess, but a google search found this answer that might be the fix.

My dependencies are {
“name”: “mongo-crud”,
“version”: “1.0.0”,
“description”: “”,
“main”: “index.js”,
“scripts”: {
“start”: “nodemon index.js”,
“test”: “echo “Error: no test specified” && exit 1”
},
“author”: “”,
“license”: “ISC”,
“dependencies”: {
“cors”: “^2.8.5”,
“express”: “^4.17.1”,
“mongodb”: “^3.6.10”
},
“devDependencies”: {
“nodemon”: “^2.0.9”
}
}

OK, so it looks like the advice in the link is appropriate. Did it work?

Should I uninstall body parser ?

I don’t know. Probably, if you aren’t using it. If express needs it as a dependency, I would think that it would install it itself as a dependency in its node module. You could also check the express documentation for that version of express. Or you could just try and see if it still works.

Here is my code.I am getting this result.Can you help me ?

const express = require(‘express’);

const MongoClient = require(‘mongodb’).MongoClient;

const password =‘sirus4213’;

const uri = “mongodb+srv://naturalUser:sirus4213@cluster0.hvcun.mongodb.net/naturaldb?retryWrites=true&w=majority”;

const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });

const app = express();

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

app.use(express.json());

app.get(’/’, (req,res) =>{

res.sendFile(__dirname + '/index.html');

})

client.connect(err => {

const productCollection = client.db(“naturaldb”).collection(“products”);

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

const product = req.body;

productCollection.insertOne(product)

.then( result => {

  res.send('success');

})

})

});

app.listen(3000);

Can you please put your code in a repo or provide a link to an online IDE? This is going to be too difficult to help without seeing the whole picture.

Also, can you POST through a browser window like that? I’ve done GET in the browser window, I don’t know that you can do a POST that way. I would expect you to do that in your program, or if you want to do it ad hoc, with a program like Postman or a service like CURL.

1 Like

bodyParser is deprecated you can use express directly i.e
app.use(bodyParser.json()) becomes app.use(express.json())

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.