【发布时间】:2021-06-08 20:19:41
【问题描述】:
在 node.js 中处理 post 请求时出现问题,并显示错误:Cannot POST /index.html 由于不推荐使用 body-parser,因此我遇到了这个错误
const express = require("express");
const app = express();
app.use(express.json({ limit: '50mb' }));
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(String(result));
});
app.listen(3000, function(){
console.log("Server is running on port 3000");
});
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></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>
【问题讨论】:
标签: javascript html node.js express