首页 > 解决方案 > 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 代码。

图片是我的 HTML 和节点 js 代码。

标签: javascriptnode.jsexpresspostget

解决方案


<!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>

解决我的问题。


推荐阅读