Lessons 244, 248 & 249 – Complete 2020 Web Development Bootcamp

Lesson 244 – BMI Routing Challenge

The challenge in this lesson is to:

  • create a new bmiCalculator.html file containing a form with an input for height and and input for weight along with a button labelled “Calculate BMI”; and
  • refactor the express code in the calculator.js file to allow the user to post their height and weight and to calculate their BMI based on those inputs.

This is the code in my bmiCalculator.html file –

<!DOCTYPE html>
<html>
<head>
	<title>BMI Calculator</title>
</head>
<body>

	<h1>BMI Calculator</h1>

	<form action="/" method="post">
		
		<input type="text" name="w" placeholder="Enter weight in pounds.">
		<input type="text" name="h" placeholder="Enter height in inches.">
		<button type="submit" name="submit">Calculate BMI</button>

	</form>

</body>
</html>

This is my refactored code in my calculator.js file –

const express = require("express");

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

const app = express();

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

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

app.post("/", function(req, res) {

	var weight = Number(req.body.w);
	var height = Number(req.body.h);

	//var calcResult = (weight / (height * height)) * 703;

	var result = Math.round(((weight / (height * height)) * 703) * 10) / 10;
	
	res.send("Your BMI is " + result);
});

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

Lesson 248 – Why do we need APIs?

An API (Application Programming Interface) is a set of commands, functions, protocols and objects that programmers can use to create software or interact with an external system.

Lesson 249 – API Endpoints, Paths & Parameters

Endpoints

An API endpoint is a point of contact in a communication channel when two systems are communicating.

For example, this http address is an API endpoint for the kanye.rest API – https://api.kanye.rest/.

Paths

The API endpoint for a joke API is – https://sv443.net/jokeapi/v2/joke/.

This particular API allows the user to select particular categories of jokes. One category is Programming. If the Programming category is selected the path to that category is – https://sv443.net/jokeapi/v2/joke/Programming.

A path is like a branch of a tree. The trunk of the tree in our example is the API endpoint – https://sv443.net/jokeapi/v2/joke/. The branch of the tree (the path) is – Programming.

Parameters

The Joke API allows you to search for specific types of jokes. For example, you can search for “debugging” jokes in the Programming category. This will give an endpoint of https://sv443.net/jokeapi/v2/joke/ with a path of Programming. It will also give parameters of “?contains=debugging”.

A parameter always begins with “?” and is a key-value pair. In the above example the key-value pair is “contains=debugging”.

If there is more than one parameter, the first parameter starts with “?” and each subsequent parameter starts with “&”. For example –

?blacklistFlags=nsfw&contains=debugging.

The entire endpoint with the path and parameters is –

https://sv443.net/jokeapi/v2/joke/Programming?blacklistFlags=nsfw&contains=debugging