BMI Calculator using Express.js - GeeksforGeeks

13 Oct, 2021
The Body Mass Index(BMI) is represented in terms of weight and height of an individual. It is the ratio of the weight of the body to the square of the height of the body in kilograms and meters respectively.
BMI = (weight of body) / (height of body)2
Unit of weight: Kilogram(Kg);
Unit of height: Meter(m);
Unit of BMI is kg/m2
Approach: 
First, we write HTML code for creating a form in which we will take name, height(m), weight(kg) as input from the user.
Import require modules and store it into app variable
sends html and post the data on the specific route using this  
app.post("/bmicalculator", function (req, res) {
    heigh = parseFloat(req.body.Height);
    weigh = parseFloat(req.body.Weight);
    bmi = weigh / (heigh * heigh);
Check Condition for BMI using formula. 
Step-1:
HTML
<!DOCTYPE html><html lang="en">    <head>        <!-- title is used to provide             a specific name to our web-page! -->        <title>BMI-CALCULATOR</title>        <!-- link function is used here, so that we can    connect our css file with our html file externally -->        <link rel="stylesheet" href="1.css" />    </head>    <body>        <div id="MAIN">            <h1 id="heading">BMI-CALCULATOR</h1>        </div>         <form action="/bmicalculator" method="post">            <input type="text"                   name="Name"                   placeholder="Enter your  name!" />            <br />            <input id="Height"                   name="Height"                   placeholder="Enter your height(m)" />            <br />            <input id="Weight"                   name="Weight"                   placeholder="Enter your weight(kg)" />            <br />            <button class="btn"                    type="submit">Get-BMI</button>        </form>    </body></html>
Output:
SIMPLE FORM
Let’s now write code for our calculations and functionalities which is the main part of our BMI-CALCULATOR.
Step-2
Dependencies: 
express:    npm install express
bodyparser:   npm install body-parser
Javascript
//importing modulesconst express = require("express");const bodyparser = require("body-parser"); // stores the express module into the app variable!const app = express();app.use(bodyparser.urlencoded({ extended: true })); //sends index.htmlapp.get("/bmicalculator", function (req, res) {    res.sendFile(__dirname + "/" + "index.html");}); //this is used to post the data on the specific routeapp.post("/bmicalculator", function (req, res) {    heigh = parseFloat(req.body.Height);    weigh = parseFloat(req.body.Weight);    bmi = weigh / (heigh * heigh);     //number to string format    bmi = bmi.toFixed();     req_name = req.body.Name;     // CONDITION FOR BMI    if (bmi < 19) {        res.send("<h3>hey! " + req_name +                 " your BMI is around: " + bmi +                 "<centre><h1>You are Underweight!");    } else if (19 <= bmi && bmi < 25) {        res.send("<h3>hey! " + req_name +                 " your BMI is around: " + bmi +                 "<centre><h1>You are Normalweight!");    } else if (25 <= bmi && bmi < 30) {        res.send("<h3>hey! " + req_name +                 " your BMI is around: " + bmi +                 "<centre><h1>You are Overweight!");    } else {        res.send("<h3>hey! " + req_name +                 " your BMI is around: " + bmi +                 "<centre><h1>You are Obese!");    }}); //this is used to listen a specific port!app.listen(7777, function () {    console.log("port active at 7777");});
Output:  
 
Final Output:
 
Output
 
sagartomar9927
saurabh1990aror
Express.js
NodeJS-Questions
Technical Scripter 2020
Node.js
Technical Scripter
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Express.js express.Router() Function
Express.js req.params Property
JWT Authentication with Node.js
Mongoose Populate() Method
Difference between npm i and npm ci in Node.js
Roadmap to Become a Web Developer in 2022
How to fetch data from an API in ReactJS ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Convert a string to an integer in JavaScript
How to insert spaces/tabs in text using HTML/CSS?