To Lecture Notes

IT 231 -- Jan 30, 2024

Review Exercises

  1. How do you create a Node project, complete with a package.json file?
    Answer: in your Command Prompt/Terminal Window, navigate to inside your project folder, for example, tutorial2c-smith. Then type npm init and answer the prompts. Hit return for the defaults.
  2. How do you install a module, such as http-status-codes, in a Node project? How do you use this module in the main.js file?
    Answer: in the Command Prompt/Terminal Window, enter this command:
    npm i http-status-nodes
    
    The i means install. Then include this link at the top of your main.js script:
    const HttpStatusCodes = require("http-status-codes");
    
    Note that variable names in JavaScript can't contain dashes, so HttpStatusCodes is used instead of http-status-codes.
  3. How do you write "The <br> tag" in a paragraph within a HTML document?
    Answer: <p>The &lt;br&gt; tag.</p>
  4. In a Node server (see the SimpleServer Example), when you are hardcoding browser content, how do you specify the type of the content in the HTML header? See the HTTP Headers document for commonly used content types.
    Answer: You specify the type of content in the HTTP header using response.writeHead:
    response.writeHead(200, {"Content-type":"text/html"});
    
    or
    response.writeHead(HttpStatusCodes.StatusCodes.OK, 
         {"Content-type":"text/html"});
    
  5. German Numbers Example. Start with this array of German numbers:
    var numbers = ["eins", "zwei", "drei", "vier", "fünf"];
    
    Modify the SimpleServer Example to output this array as an ordered list to the browser:
    1. eins
    2. zwei
    3. drei
    4. vier
    5. fünf
    Use a modern for loop to serve the contents of the array. Answer:
    // Source code file: main.js
    const PORT = 3000;
    const http = require("http");
    const app = http.createServer((req, res) => {
        console.log("Received an incoming request.");
        res.writeHead(200, {"Content-Type": "text/html" });
        var numbers = ["eins", "zwei", "drei", "vier", "fünf"];
        res.write("<head>");
        res.write('<meta charset="UTF-8">');
        res.write("</head>");
        res.write("<h1>German Numbers</h1>");
        res.write("<ol>");
        for(var n of numbers) {
            res.write(`<li>${n}</li>`);
        }
        res.write("</ol>");
        res.end( );
        console.log("Response sent.");
    });
    app.listen(PORT);
    console.log(`Server started; is listening on port ${PORT}`);
    
  6. TypewriterBox Example. Modify the SimpleServer Example to display a box like this:
    +----------+
    |          |
    |          |
    |          |
    |          |
    +----------+
    
    Enclose the box in <pre> tags; define the height of the box like this:
    const HEIGHT = 5;
    
    <pre> tags define preformatted text, where text is displayed using a fixed-width font with spaces and line breaks preserved. Answer:
    // Source code file: main.js
    var http = require("http");
    const app = http.createServer((req, res) => {
        console.log("Received an incoming request.");
        res.writeHead(200, {"Content-Type": "text/html" });
        res.write("<h1>Typewriter Graphics Box</h1>");
        const HEIGHT = 5;
        res.write("<pre>\n");
        res.write("+----------+\n");
        for(var i = 1; i <= HEIGHT; i++) {
            res.write("|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|\n");
        }
        res.write("+----------+\n");
        res.write("</pre>");
        res.end( );
        console.log("Response sent.");
    });
    app.listen(3000);
    console.log("Server started; is listening on port 3000");
    
  7. What are the response methods that are used to write to the localhost browser?
    Ans: writeHead write end
  8. How can you get the following statements to work?
    console.log("This is
    a test.");
    
    Answer: you can either use the \n entity like this:
    console.log("This is\na test.");
    
    or use backquotes like this:
    console.log(`This is
    a test.`);
    

Practice Quiz 4a

Tutorial 2c

Object Literals Example

JSON (JavaScript Object Notation) Example

HTTP

HTTP Request-response Cycle