html - 我头写了node js代码,但是post方法不起作用但get方法起作用,为什么?
问题描述
html部分
<!DOCTYPE html>
<html lang="en">
<head>
<title>Calculator</title>
</head>
<body>
<h1>Calculator</h1>
<form action="/index.html" method="POST">
<input type="text" name="num1" placeholder="First Number">
<input type="text" name="num2" placeholder="Second Number">
<button type="submit" name="submit">Calculate</button>
</form>
</body>
</html>
节点js部分
const express = require("express");
const bP = require("body-parser");
const app = express();
app.use(bP.urlencoded({extended: true}));
app.get("/",function(req,res){
res.sendFile(__dirname + "/index.html");
});
app.post("/",function(req,res){
res.send("Thanks for posting that");
});
app.listen(3000,function(){
console.log("The server is started on port 3000.");
});
index.html 文件包含两个带有两个数字的输入字段和一个提交按钮。单击提交按钮时,我得到了输出
无法发布 /index.html
解决方案
我刚刚更正了 app.post() 方法
改正前是这样的:
res.send("Thanks for posting that");
});```
After correction:
```app.post("/index.html",function(req,res){
res.send("Thanks for posting that");
});```
推荐阅读
- haskell - 为什么 ghc 8.8.4 无法编译 ghci 可以加载的文件?
- laravel - Laravel Spark - 缺少 PHP 文件 PreventRequestsDuringMaintenance
- java - Netty 解码器在写入字符串时抛出 IndexOutOfBounsException
- apache-spark - SparkSql if value null 取前一个
- r - 闪亮的ggplotly图的动态高度
- javafx - 带有 javafx 启动的 InvocationTargetException
- php - 如何将用户输入存储在变量上以将其用作 bat 文件中的参数?
- swift - 更改静态地图图像中折线的颜色
- javascript - 为什么即使未单击按钮,在子组件 onclick 中设置状态也会导致递归循环?
- c# - 实现fire and forget异步方法调用的正确方法