BMI Routing

vancouver·2023년 5월 5일
0

BMI Calculator Page

계산기 페이지에 BMI 계산하는 페이지 추가 생성하는 과정

HTMl

<!DOCTYPE html>
<html lang="ko">
<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>
    <h1>BMI Calculator</h1>
    <form action="/bmiCalculator" method="post">
        <input type="text" name="weight" placeholder="your weight">
        <input type="text" name="height" placeholder="your height(m)">
        <button type="submit">Calculate BMI</button>
    </form>
</body>
</html>

javascript

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


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) );
    res.send("Your BMI is" + bmi)
});

추가설명

 <form action="/bmicalculator" method="post">  - bmiCalculator라는 주소를 생성
app.get("/bmicalculator") // html파일에 form의 주소와 같게 해야함
app.post("/bmicalculator")// html파일에 form의 주소와 같게 해야함

Result

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

const app = express();

app.use(bodyParser.urlencoded({extended: true}));

//Calculator
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 of the calculation is" + result)

});

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


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) );
    res.send("Your BMI is" + bmi)
});

app.listen(3000, function(){
    console.log("Server started on port 3000") 
 });

0개의 댓글