javascript - POST 请求在 nodejs 中无法使用 expressjs 和 bodyparser
问题描述
我是nodejs的新手,目前正在学习。我遇到了我的 POST req 不起作用的问题。我制作了一个简单的 html 表单来执行 GET 和 POST 请求。但是,GET req 工作正常。如何使 POST req 在浏览器上运行或这里有什么问题?我真的很感谢专家的一些帮助/指导。谢谢你。
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.get("/", function(req, res){
res.sendFile(__dirname + "/index.html");
});
app.post("/", function(req, res){
res.send("thanks for posting");
});
app.listen(3000, function(){
console.log("the port is working just fine");
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Calculator</title>
</head>
<body>
<h1>Calculator</h1>
<form action="/" method="post"></form>
<input type="text" name="num1" placeholder="first number">
<input type="text" name="num2" placeholder="second number">
<button type="submit" name="submit">Calculator</button>
</body>
</html>
图片是我的 HTML 和节点 js 代码。
解决方案
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Calculator</title>
</head>
<body>
<h1>Calculator</h1>
<form action="/" method="post">
<input type="text" name="num1" placeholder="first number">
<input type="text" name="num2" placeholder="second number">
<button type="submit" name="submit">Calculator</button>
</form>
</body>
</html>
解决我的问题。
推荐阅读
- c# - c# LINQ 过滤器嵌套集合
- swift - 日期()之间有什么区别。和日期。在斯威夫特?
- azure - Azure Integration Account, use public and private certificate in agreement
- git - 修改后rebase分支?
- c# - Firebase unity authentication google signIn,下载包去掉Unity项目的UI
- java - 如何使用 presto-parser 获取 presto 查询中引用的所有数据库表
- jmeter - 为什么在“jp@gc - Active Threads Over Time”中显示的用户数量存在差异
- javascript - Netsuite 套件响应式 UI,站点构建器捆绑
- java - 使用 configMap 和 Volumes 从 kubernetes 读取配置文件
- php - PHP - MYSQL 连接问题