Jump to content

Server-Side Scripting/Iteration/Node.js (Express)

From Wikiversity

routes/lesson4.js

[edit | edit source]
// Demonstrates conditions, while loops and for loops using
// Celsius and Fahrenheit temperature conversion tables.
//
// References:
//  https://www.mathsisfun.com/temperature-conversion.html
//  https://en.wikibooks.org/wiki/JavaScript
//  https://stackabuse.com/get-query-strings-and-parameters-in-express-js/
//  https://flaviocopes.com/express-forms/
//  https://expressjs.com/en/guide/routing.html

const express = require('express')
const fs = require("fs");
const handlebars = require('handlebars');
const router = express.Router()

router.get("/", function (request, response) {
    let source = fs.readFileSync("./templates/lesson4.html");
    let template = handlebars.compile(source.toString());
    let data = {
        table: ""
    }
    result = template(data);
    response.send(result);
});

router.post("/", function (request, response) {
    let start = Number(request.body.start);
    let stop = Number(request.body.stop);
    let increment = Number(request.body.increment);
    let submit = request.body.submit;

    let result = "";
    if (submit == "Celsius") {
        result = processCelsius(start, stop, increment);
    }
    else if (submit == "Fahrenheit") {
        result = processFahrenheit(start, stop, increment);
    }
    else {
        result = "Unexpected submit value: " + submit;
    }

    let source = fs.readFileSync("./templates/lesson4.html");
    let template = handlebars.compile(source.toString());
    let data = {
        table: result
    }
    result = template(data);
    response.send(result);
});

function processCelsius(start, stop, increment) {
    let result = "<table><tr><th>Celsius</th><th>Fahrenheit</th></tr>";
    let celsius = start;
    while (celsius <= stop) {
        let fahrenheit = celsius * 9 / 5 + 32;
        result += "<tr><td>" + celsius + "</td>";
        result += "<td>" + fahrenheit.toFixed(1) + "</td></tr>";
        celsius += increment;
    }
    result += "</table>";
    return result;
}

function processFahrenheit(start, stop, increment) {
    let result = "<table><tr><th>Fahrenheit</th><th>Celsius</th></tr>";
    for (let fahrenheit = start; fahrenheit <= stop; fahrenheit += increment) {
        let celsius = (fahrenheit - 32) * 5 / 9;
        result += "<tr><td>" + fahrenheit + "</td>";
        result += "<td>" + celsius.toFixed(1) + "</td></tr>";
    }
    result += "</table>";
    return result;
}

module.exports = router;

templates/lesson4.html

[edit | edit source]
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Lesson 4</title>
    <link rel="stylesheet" href="styles.css">
    <style>
        p {
            min-height: 1em;
        }
    </style>
</head>

<body>
    <h1>Temperature Conversion</h1>
    <form method="POST">
        <p><label for="start">Start:</label>
            <input type="text" id="start" name="start" value="0">
        </p>
        <p><label for="stop">Stop:</label>
            <input type="text" id="stop" name="stop" value="100">
        </p>
        <p><label for="increment:">Increment</label>
            <input type="text" id="increment" name="increment" value="10">
        </p>
        <p><input type="submit" name="submit" value="Celsius">
            <input type="submit" name="submit" value="Fahrenheit">
        </p>
    </form>
    {{{table}}}
</body>

</html>

Try It

[edit | edit source]

See Server-Side Scripting/Routes and Templates/Node.js (Express) to create a test environment.