Shows error “Cannot POST” when I try to submit the HTML form Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Shows error “Cannot POST” when I try to submit the HTML form without wasting too much if your time.

The question is published on by Tutorial Guruji team.

HTML code work fine. Here is my JS code:

 // Node.js JavaScript code

 const express = require ("express");
  const bodyParser = require("body-parser");

   const app = express();

    app.use(bodyParser.urlencoded({extended:true}));

 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("the result is"+ result);

});


 app.listen(3000,function(){
  console.log("starting");
  });

I have seen many similar question but none of them worked. Packages are properly installed.

Answer

I guess this error appears because you’re trying to post a different route in your index.html

Please give it a try and let me know if it works. It would be helpful if you post the full error message.

We are here to answer your question about Shows error “Cannot POST” when I try to submit the HTML form - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji