首页 > 解决方案 > 我头写了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

标签: htmlnode.jsexpress

解决方案


我刚刚更正了 app.post() 方法

改正前是这样的:

    res.send("Thanks for posting that");
});```

After correction:
```app.post("/index.html",function(req,res){
    res.send("Thanks for posting that");
});```

推荐阅读