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

From Wikiversity
Jump to navigation Jump to search

app.js[edit | edit source]

// This program reads a user-selected text file of countries
// and Celsius temperatures. It displays the data in Celsius
// and Fahrenheit sorted in decending order by temperature.
//
// File format:
// Country,Temperature
// American Samoa,37.2° C
// Bulgaria,45.2° C
//
//  https://www.mathsisfun.com/temperature-conversion.html
//  https://en.wikibooks.org/wiki/JavaScript
//  https://www.npmjs.com/package/express-fileupload

const express = require('express');
const fileUpload = require('express-fileupload');
const app = express();

const FORM = `
<h1>Temperature Conversion</h1>
<p>Select a comma-separated file of countries and Celsius temperatures:</p>
<form method="POST" enctype="multipart/form-data">
<input type="file" id="file" name="file">
<input type="submit">
</form>
`

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

app.use(fileUpload({
  limits: { fileSize: 1 * 1024 * 1024 },
}));

app.get('/', (request, response) => {
    response.send(FORM);
});

app.post('/', (request, response) => {
    let result = "";

    if (!request.files || Object.keys(request.files).length == 0) {
        result = "No file selected"
    } else {
        let file = request.files.file;
        result = "<h1>Temperature Conversion</h1>";
        result += "<h2>" + file.name + "</h2>";
        result += processFile(file)
    }

    response.send(result);
});

function processFile(file) {
    let text = file.data.toString();
    let lines = text.trim().split("\n");
    if (lines[0].toLowerCase().indexOf("country") >= 0) {
        lines.shift(); // remove heading line
    }

    let table = [];
    for (let index = 0; index < lines.length; index++) {
        try {
            let record = processLine(lines[index]);
            table.push(record);
        }
        catch(error) {
            return error;
        }
    }

    table.sort(function(a, b) {return b.celsius - a.celsius});
    result = formatTable(table);
    return result
}

function processLine(line) {
    let record = {};
    let array = line.split(",");
    if (array.length != 2) {
        throw "Invalid file format"
    }

    record.country = array[0];
    let celsius = array[1];
    let index = celsius.indexOf("° C");
    if (index < 0) {
        throw "Invalid file format";
    }

    celsius = Number(celsius.substring(0, index));
    record.celsius = celsius;
    let fahrenheit = celsius * 9 / 5 + 32;
    record.fahrenheit = fahrenheit
    return record;
}

function formatTable(table) {
    let result = "<table><tr><th>Country</th>"
    result += "<th>Celsius</th>";
    result += "<th>Fahrenheit</th></tr>";

    for (index = 0; index < table.length; index++) {
        let record = table[index];
        result += "<tr><td>" + record.country + "</td>";
        result += "<td>" + record.celsius.toFixed(1) + "° C</td>";
        result += "<td>" + record.fahrenheit.toFixed(1) + "° F</td></tr>";        
    }

    result += "</table>";
    return result;
}

app.listen(3000, () => console.log('server started'));

Try It[edit | edit source]

Copy and paste the code above into the following free online development environment or use your own Node.js (Express) compiler / interpreter / IDE.