To Lecture Notes

IT 231 -- Feb 13, 2024

Review Exercises

  1. What are some of the similarities and differences of Express.js vs. Node.js? Answer:
    1. Express is a web framework that runs on top of Node.
    2. Express has more features and is easier to use than Node.
    3. Node is written in JavaScript, C, and C++; Express is written JavaScript.
    4. With Express, routing is provided with the app.get and app.post methods; with Node routing is not provided.
    5. Serving static routes is easier with Express.
  2. What are the request and response object methods that you know using an Express server defined as
    const app = express( );
    
    Answer: We have only seen one response method:
    res.send
    
    This method sends a string to display in the browser. Later today, we will use the res.render method, which displays an Embedded JavaScript (.ejs) file from the views folder in the browser. We have not seen any Express request methods, but we will see some soon.
  3. Load the JSON string from the file world-cities.json into the string citiesJSON like this:
    const fs = require("fs");
    fs.readFile('world-cities.json', (err, data) => {
        if (err) {
            console.log("Error reading file " +
                "world-cities.json");
        }
        else {
            var citiesJSON = JSON.parse(data);
            // Add your own code here
            // to obtain the answers for
            // exercises 6a, 6b, and 6c.
        }
    });
    
    This JSON file contains data for all cities in the world with population 10,000 persons or more. Now convert this string to the array of objects cities and answer these questions:
    1. List all cities in the subcountry Wyoming of the country United States. (Wyoming is the state with fewest cities with population over 10,000, so the answer is easy to check.
    2. List the country and subcountry of all cities named Springfield.
    3. How many cities are in the JSON file?
    Answer: Here is the JavaScript code for parts 3a, b, and c:
     // Part 3a. List all cities in Wyoming.
    console.log("Cities in Wyoming");
    for(var c of citiesJSON) {
        if (c.country == "United States" &&
            c.subcountry == "Wyoming") {
            console.log(c.name);
        }
    }
    
    // Part 3b. List the subcountry and country of
    // all cities named Springfield.
    console.log("\nCities named Springfield.")
    for(var c of citiesJSON) {
        if (c.name == "Springfield") {
            console.log(c.subcountry + ", " +
            c.country);
        }
    }
    
    // Part 3c. Display the number of cities over 10,000 population.
    console.log("\nCount of cities over 10,000 population");
    console.log(citiesJSON.length);
    

Nodemon

Practice Quiz 5a

Project 2

Tutorial 4

Static Routes

Hyperlinks

Images and External Stylesheets on Webpages