rahul sarode
rahul sarode

Reputation: 5

Node.js (express framework): Post method not working

I am able to run the Get method and HTML form also visible on browser using Get method but when I am clicking on submit button nothing is happening, no error nothing. it shows the same HTML form page.

HTML code:
  <!DOCTYPE html>enter code here
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Calculator</title>
  </head>
  <body>
<form action="/" method="post">
  <h1>Calculator</h1>
  <input type="text" name="num1" placeholder="Number 1">
  <input type="text" name="num2" placeholder="Number 2">
  <button action="/" type="button" name="button">Submit</button>
</form>
  </body>
</html>

Node Js code:

//jshint esversion:6

const express=require("express");
const app=express();

app.get("/", function(req,res){
res.sendFile( __dirname+"/index.html");
});

app.post("/", function(req,res){
  res.send("Thanks for post");
});

app.listen(3000, function(){
  console.log("Server Started On Port 3000");
});

Upvotes: 0

Views: 150

Answers (1)

r7r
r7r

Reputation: 1525

Your button type should be Submit

    <button type="submit" value="Submit">Calculator</button>

Upvotes: 1

Related Questions