首页 > 解决方案 > bodyParser 被删除不能在 express 中 POST 错误

问题描述

我的导师代码

这是我的代码,但是名为 bodyParser 的 var 被删除了我不知道为什么而且我的计算器不工作,说不能 POST /index.html 这个计算器用于添加两个数字 这是我的代码

我的代码

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

const app = express();
app.use(<strike>**bodyParser**</strike>.urlencoded({ extended: true }));

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

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

  var result = num1 + num2;

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

app.listen(4040, function () {
  console.log("Server started at port 4040");
});

标签: javascriptnode.jsexpressvisual-studio-codecalculator

解决方案


要使用 body-parser,您必须先安装它,您可以通过 Hyper bash 进行安装

cd <到您的 .js 文件所在的文件夹>

npm 安装正文解析器

检查 package.json 以确保安装了 body-parser


推荐阅读