/* * CSE 154 Summer 2019 * Solution API for printing "Hello World" and doing various math operations. * /^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v/ * Endpoint: /hello * Response Format: text * Prints "Hello World!" to the client. * /^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v/ * Endpoint: /hello/name * Query Parameters: "first" - Someone's first name, "last" - Someone's last name. * Response Format: text * Responds with a hello to the given first/last named person. * /^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v/ * Endpoint: /math/circle/:r * Response Format: JSON * Response Example: {"area": 3.14, "circumference": 6.28} * Returns the area and circumference of a circle with radius :r. * /^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v/ * Endpoint: /math/rectangle/:width/:height * Response Format: JSON * Response Example: {"area": 6, "perimeter": 16} * Returns the area and perimeter of a rectangle with given :width and :height * /^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v/ * Endpoint: /math/power/:base/:exponent * Response Format: JSON * Response Example {"result": 8} * Returns the :base raised to the :exponent */ "use strict"; const express = require("express"); const app = express(); app.get("/hello", function(req, res) { res.set("Content-Type", "text/plain"); res.send("Hello World!"); }); app.get("/hello/name", function(req, res) { res.set("Content-Type", "text/plain"); if (req.query["first"] && req.query["last"]) { res.send("Hello " + req.query["first"] + " " + req.query["last"]); } else { res.status(400).send("Error: missing first or last names."); } }); app.get("/math/circle/:r", function(req, res) { res.set("Content-Type", "application/json"); let radius = parseFloat(req.params["r"]); let area = Math.PI * radius * radius; let circumference = Math.PI * radius * 2; res.json({"area": area, "circumference": circumference}); }); app.get("/math/rectangle/:width/:height", function(req, res) { res.set("Content-Type", "application/json"); let width = parseFloat(req.params["width"]); let height = parseFloat(req.params["height"]); let area = width * height; let perimeter = (width + height) * 2; res.json({"area": area, "perimeter": perimeter}); }); app.get("/math/power/:base/:exponent", function(req, res) { res.set("Content-Type", "application/json"); let base = parseFloat(req.params["base"]); let exponent = parseFloat(req.params["exponent"]); let power = Math.pow(base, exponent); res.json({"result": power}); }); app.use(express.static("public")); const PORT = process.env.PORT || 8000; app.listen(PORT);