Home > Software engineering >  Not getting response calculation from express server?
Not getting response calculation from express server?

Time:07-21

just tell me what is missing in my code?

here is my html code:-

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Calculator</title>
    <!-- <link rel="stylesheet" href="styles.css"> -->
    <style media="screen">
      body {background-color: powderblue;}
    </style>
  </head>
  <body>
    <h1>Calculator</h1>
    <form action="/" method="post">
      <input type="text" name="n1" placeholder="First Number">
      <input type="text" name="n2" placeholder="Second Number">
      <button type="button" name="button">Calculate</button>

    </form>
  </body>
</html>

Here is my Express js code:-

// jshint esversion:6

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.n1);
  var num2 = Number(req.body.n2);
  var result = num1   num2;
  res.send("The result is: "   result)
});
app.listen(3000, function(){
  console.log("App is running on port 3000");
});

I have imported body-parser. But still not working. I want to get output as the Result on screen.

CodePudding user response:

Use either

<button>Calculate</button>

(without the type attribute), or use

<input type="submit" value="Calculate"/>

Your <button type="button"> does not trigger a submission of the form.

CodePudding user response:

<button type="button" name="button">Calculate</button>

to

<button type="submit" name="button">Calculate</button>

work for me

  • Related