-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.js
45 lines (35 loc) · 1.13 KB
/
calculator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const express = require('express');
const app = express();
app.use(express.urlencoded({extended:true}));
// app.get('/', (req, res) => {
// res.send('Hello World!')
// })
app.get("/", function(req, res){
//res.send('Hello World!')
res.sendFile(__dirname + "/index.html")
//console.log(__dirname);
});
app.post("/", function(req,res){
//console.log(req.body)
// num1 is the name of the field in form in index.html
var num1 = req.body.num1; // parsed at text
num1 = Number(num1);
var num2 = Number(req.body.num2);
var result = num1 + num2;
res.send("The result of the calculation is "+result+ ".")
})
// using server, we no longer access using bmiCalculator.html
// just use route names
app.get("/bmicalculator", function(req, res){
//res.send('Hello World!')
res.sendFile(__dirname + "/bmiCalculator.html")
//console.log(__dirname);
});
app.post("/bmiCalculator", function(req,res){
console.log(req.body)
var weight = parseFloat(req.body.weight);
var height = parseFloat(req.body.height);
var result = weight / (height*height);
res.send("The result of the bmi calculation is "+result+ ".")
})
app.listen(3000);