Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Savy011/fullstackopen-exercises. My Exercise Submissions for FullStack Open fullstackopen.com/en
Something went wrong. Try again.
1.4 kB · 57 lines
TypeScript
at main
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758import express from 'express';const app = express();
import { calculateBmi } from './bmiCalculatorExpress';import { calculateExercises } from './exerciseCalculatorExpress';
app.use(express.json());
app.get('/hello', (_req, res) => { res.send('Hello Full Stack!');});
app.get ('/bmi', (req, res) => { const { height, weight } = req.query;
if (!height || !weight) { return res.status(400).send({ error: 'Malformatted or Missing Parameters' }); } try { const { bmi, bmiRemark } = calculateBmi(Number(height), Number(weight)); return res.status(200).json({ height, weight, bmi, bmiRemark}); } catch (error: unknown) { if (error instanceof Error) { return res.status(400).send({ error: error.message }); } return res.status(400).json({ error: 'Some Error Occured' }); }});
app.post('/exercises', (req, res) => { const { daily_exercises , target } = req.body;
if (!daily_exercises || !target) { return res.status(400).json({ error: 'Malformatted or Missing Parameters' }); } try { const result = calculateExercises(daily_exercises, Number(target)); return res.status(200).json(result); } catch (error) { if (error instanceof Error) { return res.status(400).json({ error: error.message }); } return res.status(400).json({ error: 'Some Error Occured' }); }});
const PORT = 3002;
app.listen(PORT, () => { console.log(`Server running at Port ${PORT}`);});