const app = express( );what do these methods do?
(a) app.get (b) app.set (c) app.useAnswer:
app.get(route, callback);If the route input into the URL field of the browser matches the route parameter, the callback function is executed.
app.set(key, value);For example,
app.set("view engine", "ejs");adds a setting to the browser app with key "view engine" and value "ejs".
app.use(express.static("public"));This method call sets the public folder as a static folder.
reverse toString sort forEach (hypermodern for loop)Here are three for loops that print the items in an array:
// Array definition: var arr = [4, 2, 7, 6, 8]; // Traditional for loop: for(let i = 0; i < arr; i++) { console.log(arr[i]); } // Modern for loop: for(let n of arr) { console.log(n); } // Hypermodern for loop: arr.forEach( n => { console.log(n); });We also discussed the Array length property.
var i = a.indexOf("page"); // or a.sort( );The difference in accessing an Array method vs. an Array property:
push pop shift unshift slice spliceTry out these methods using REPL. Answer: Here is our REPL session from class:
> var a = [3, 6, 4, 5, 7]; undefined > a.pop( ) 7 > a [3, 6, 4, 5] > var a = [3, 6, 4, 5, 7]; undefined > a.push(9); 6 > a [3, 6, 4, 5, 7, 9] > var a = [3, 6, 4, 5, 7]; undefined > a.shift( ) 3 > a [6, 4, 5, 7] > var a = [3, 6, 4, 5, 7]; undefined > a.unshift(9); 6 > a [9, 3, 6, 4, 5, 7] > a.shift( ); 9 > a [3, 6, 4, 5, 7] > a.splice(1, 3, 97, 98] [6, 4, 5] > a [3, 97, 98, 7]
const express = require("express"); const app = express( ); // Set View Engine to EJS app.set("view engine", "ejs"); app.get("/", (q, r) => { r.render("index"); }); app.listen(3000, ( ) => { console.log("Listening on Port 3000."); });
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <h1>I Owe Larry</h1> <% let name = "Larry"; let amt = 258.61 %> <p><%= name %> owes me $<%= amt %>.</p> </body> </html>
localhost:3000/
------------------------------------------------- // Modified main.js script: const express = require("express"); const app = express( ); // Set View Engine to EJS app.set("view engine", "ejs"); // Set the data to pass to the EJS page: var firstName = "Larry"; var amtOwed = 258.61; // The previous two lines can be omitted if // the values of firstName and amtOwed are hardcoded // into the r.render statement as shown on the line // following it. // Execute if requested route is /. app.get("/", (q, r) => { r.render("index", { name: firstName, amt: amtOwed }); // r.render("index", name: "Larry", amt: 258.51 }); }); app.listen(3000, ( ) => { console.log("Listening on Port 3000."); }); ------------------------------------------------- <!DOCTYPE html> <!-- Modified EJS file index.ejs. --> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <h1>I Owe Larry</h1> <p><%= name %> owes me $<%= amt %>.</p> </body> </html> -------------------------------------------------
localhost:3000/
localhost:3000/prediction