0

I am trying to use Bodyparser in my code but the code is cut by the editor itself and says body-parser is deprecated. I tried some alternative codes I found online but none of those are working. Attached is the screenshot of the code I am using.Code

Code:

const express = require("express");
const bodyParser = require("body-parser");

const app = express()
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

app.get("/", function(req, res){
res.sendFile(__dirname + "/index.html");
});

app.post("/", function(req, res){
    var num1 = req.body.num1;
    var num2 = req.body.num2;
    var result = num1 + num2;

    res.send("The result of the calculation is " + result);
});

app.listen(3000, function(){
console.log("Server has started on port3000");
});
  • express has its own body parsers - read the docs - or [this](https://medium.com/@mmajdanski/express-body-parser-and-why-may-not-need-it-335803cd048c) – Bravo Jul 29 '21 at 00:43
  • 2
    Does this answer your question? [bodyParser is deprecated express 4](https://stackoverflow.com/questions/24330014/bodyparser-is-deprecated-express-4) – KingJulian Jul 29 '21 at 01:18

1 Answers1

1

You could find it easily, there a lot of answers about it. Just use express for it

app.use(express.json());

Daniel
  • 97
  • 10