简体   繁体   中英

POST request not working in nodejs with expressjs and bodyparser

I am new into nodejs and currently learning. I came across the problem where my POST req is not working. I made a simple html form to exercise the GET and POST req. However, the GET req is just working fine. How can I make the POST req run on browser or what is the issue here? I would really appreciate some help/guidance from the experts. thank you.

 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>

images are my HTML and node js codes.

图片是我的 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>

the fixation to my issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM