【问题标题】:Body Parser is deprecated [duplicate]不推荐使用正文解析器[重复]
【发布时间】:2021-10-04 16:37:41
【问题描述】:

我试图在我的代码中使用 Bodyparser,但代码被编辑器本身剪切并说 body-parser 已被弃用。我尝试了一些在网上找到的替代代码,但没有一个有效。 附上我正在使用的代码截图。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");
});

【问题讨论】:

标签: javascript node.js express node-modules body-parser


【解决方案1】:

你很容易找到它,有很多关于它的答案。 只需使用 express 即可

app.use(express.json());

【讨论】:

  • 如果有很多现有答案(可能是其他问题),请将此标记为重复。
猜你喜欢
  • 2023-03-22
  • 2015-08-10
  • 1970-01-01
  • 1970-01-01
  • 2013-11-03
  • 2012-11-14
  • 1970-01-01
  • 2015-07-05
  • 1970-01-01
相关资源
最近更新 更多