javascript - 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");
});
解决方案
要使用 body-parser,您必须先安装它,您可以通过 Hyper bash 进行安装
cd <到您的 .js 文件所在的文件夹>
npm 安装正文解析器
检查 package.json 以确保安装了 body-parser
推荐阅读
- javascript - 那么这个诺言是如何运作的呢?
- python - matplotlib rc('text', usetex=True) 产生错误
- android - IntelliJ 模拟器无法正确扩展
- javascript - 如何在子类主体中声明时访问和扩展 ES7 父类属性?
- angular - NgZone 或 ChangeDetectorRef 在 Ionic 3 中不起作用
- python - 使用python的子进程时缺少环境变量
- javascript - 当我通过反应应用程序发出请求时,我的网络服务器发送 404,但是当我直接通过 chrome 或邮递员访问它时工作正常
- jquery - URL 编码在 Yii2 和 jQuery 中不起作用
- dart - 在 Dart (2.0.0-dev.62.0) 中调用泛型函数
- javascript - 从 d3-tile 检索地理坐标