Калькулятор ИМТ с использованием Express.js
Опубликовано: 26 Июля, 2021
Индекс массы тела (ИМТ) представлен в виде веса и роста человека. Это отношение веса тела к квадрату высоты тела в килограммах и метрах соответственно.
ИМТ = (вес тела) / (рост) 2 Единица веса: килограмм (кг); Единица высоты: метр (м); Единица ИМТ - кг / м 2.
Подход:
- Сначала мы пишем HTML-код для создания формы, в которой мы будем принимать от пользователя имя, рост (м), вес (кг).
- Импортируйте требуемые модули и сохраните их в переменной приложения.
- отправляет html и публикует данные по конкретному маршруту, используя этот
app.post ("/ bmicalculator", function (req, res) { высота = parseFloat (req.body.Height); weight = parseFloat (req.body.Weight); весить = parseFloat (req.body.Weight); bmi = вес / (высота * высота);
- Проверьте состояние для ИМТ с помощью формулы.
Шаг 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 > |
Выход:
Давайте теперь напишем код для наших вычислений и функций, которые являются основной частью нашего BMI-CALCULATOR.
Шаг 2
Зависимости: экспресс: npm установить экспресс bodyparser: npm установить body-parser
Javascript
//importing modules const 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.html app.get( "/bmicalculator" , function (req, res) { res.sendFile(__dirname + "/" + "index.html" ); }); //this is used to post the data on the specific route app.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" ); }); |
Выход:
Окончательный вывод: