-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
41 lines (33 loc) · 884 Bytes
/
index.ts
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
import express from "express";
import { calculateBmi } from "./bmiCalculator";
//const express = require("express");
const app = express();
app.get("/hello", (_req, res) => {
res.send("Hello Full Stack!");
});
app.get("/bmi", (req, res) => {
//get and validate params
if (
!req.query.height ||
!req.query.weight ||
isNaN(Number(req.query.height)) ||
isNaN(Number(req.query.weight))
) {
res.status(400).send({ error: "malformatted parameters" });
}
const weight = Number(req.query.weight);
const height = Number(req.query.height);
if (height <= 0 || weight <= 0) {
res.status(400).send({ error: "malformatted parameters" });
}
const result = calculateBmi(height, weight);
res.send({
weight,
height,
bmi: result,
});
});
const PORT = 3003;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});