Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

131
Views
post method in express.js not working for BMI calculator

i am creating a simple BMI calculator app for JavaScript practice, which takes two inputs weight and height. but after providing inputs and submitting the web page doesn't show calculated BMI. also in devtools network it shows request method get. i have attached the related code. please help!!

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

const app = express();
app.use(bodyParser.urlencoded({extended: true})); // for input data coming from html form

// home route for calculator
 app.get("/", (req, res)=>{
     res.sendFile(__dirname+"/index.html");  //sending index.html as a response using res.sendFile method 
 })

 //processing post request for calculator
 app.post("/", (req, res)=>{
    var num1 =Number(req.body.num1);
    var num2 =Number(req.body.num2);
    var total = num1 + num2;
    res.send(`<h1>the total is ${total}</h1>`);
 })


 // route for get request for bmicalculator
 app.get("/bmicalculator", function(req, res){
     res.sendFile(__dirname+"/BMIcalculator.html");
 })

// processing post request and returning calculated BMI
app.post("/bmicalculator", function(req, res){
    var weight = parseFloat(req.body.weight);
    var height = parseFloat(req.body.height);
    var BMI = weight / Math.pow(height, 2);
    console.log(BMI);
    res.send(BMI);
})

app.listen(3000, ()=>{
    console.log("server is listening at port 3000");
})
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>BMI calculator</title>
</head>
<body style="background-color: darkkhaki;">
    <h1>calculate your BMI</h1>
    <form action="/bmicalculator" method="post"></form>
    <input type="text" name="weight" placeholder="enter your weight">
    <input type="text" name="height" placeholder="enter your height in meters">
    <button type="submit" name="Submit">calculate BMI</button>
    
</body>
</html>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Keep your INPUT tags inside FORM tag then only request went to server and you will receive response back.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!