diff --git a/01-node-tutorial/answers/01-intro.js b/01-node-tutorial/answers/01-intro.js new file mode 100644 index 000000000..95fe0a412 --- /dev/null +++ b/01-node-tutorial/answers/01-intro.js @@ -0,0 +1 @@ +console.log("Hello World!!"); diff --git a/01-node-tutorial/answers/02-globals.js b/01-node-tutorial/answers/02-globals.js new file mode 100644 index 000000000..804947595 --- /dev/null +++ b/01-node-tutorial/answers/02-globals.js @@ -0,0 +1,2 @@ +console.log(`Current Directory: ${__dirname}`); +console.log(`MY_VAR Global Variable: ${process.env.MY_VAR}`); diff --git a/01-node-tutorial/answers/03-modules.js b/01-node-tutorial/answers/03-modules.js new file mode 100644 index 000000000..9c94dbeef --- /dev/null +++ b/01-node-tutorial/answers/03-modules.js @@ -0,0 +1,14 @@ +const names = require("./04-names.js"); +const sayWazzup = require("./05-utils.js"); +const familyGuy = require("./06-alternative-flavor.js"); +const mindGrenade = require("./07-mind-grenade.js"); + +console.log("King of the Hill family:"); +for (key in names) { + console.log(names[key]); +} + +console.log("Family Guy characters:"); +for (key in familyGuy) { + sayWazzup(familyGuy[key]); +} diff --git a/01-node-tutorial/answers/04-names.js b/01-node-tutorial/answers/04-names.js new file mode 100644 index 000000000..c760ab69f --- /dev/null +++ b/01-node-tutorial/answers/04-names.js @@ -0,0 +1,8 @@ +const names = { + dad: "Hank Hill", + son: "Bobby Hill", + wife: "Peggy Hill", + dog: "Lady Bird", +}; + +module.exports = names; diff --git a/01-node-tutorial/answers/05-utils.js b/01-node-tutorial/answers/05-utils.js new file mode 100644 index 000000000..31a1787ff --- /dev/null +++ b/01-node-tutorial/answers/05-utils.js @@ -0,0 +1,5 @@ +function sayWazzup(name) { + console.log(`Hey ${name} Waaaazzzuuuuup`); +} + +module.exports = sayWazzup; diff --git a/01-node-tutorial/answers/06-alternative-flavor.js b/01-node-tutorial/answers/06-alternative-flavor.js new file mode 100644 index 000000000..cdfb4a7d3 --- /dev/null +++ b/01-node-tutorial/answers/06-alternative-flavor.js @@ -0,0 +1,8 @@ +const dad = "Peter Griffin"; +module.exports.dad = dad; + +const mom = "Louis Griffin"; +module.exports.mom = mom; + +const dog = "Brian Griffin"; +module.exports.dog = dog; diff --git a/01-node-tutorial/answers/07-mind-grenade.js b/01-node-tutorial/answers/07-mind-grenade.js new file mode 100644 index 000000000..bad999677 --- /dev/null +++ b/01-node-tutorial/answers/07-mind-grenade.js @@ -0,0 +1,5 @@ +function printCurrentDir() { + console.log(`Currently in directory: ${__dirname}`); +} + +printCurrentDir(); diff --git a/01-node-tutorial/answers/08-os-module.js b/01-node-tutorial/answers/08-os-module.js new file mode 100644 index 000000000..91a1f824e --- /dev/null +++ b/01-node-tutorial/answers/08-os-module.js @@ -0,0 +1,5 @@ +const os = require("os"); + +console.log(`The default system directory is: ${os.homedir()}`); +console.log(`The system kernel version is: ${os.version()}`); +console.log(`The operating system is: ${os.type()}`); diff --git a/01-node-tutorial/answers/09-path-module.js b/01-node-tutorial/answers/09-path-module.js new file mode 100644 index 000000000..00d2a8a6a --- /dev/null +++ b/01-node-tutorial/answers/09-path-module.js @@ -0,0 +1,7 @@ +const path = require("path"); +const os = require("os"); + +const homeDir = os.homedir(); + +const filePath = path.join(homeDir, "Documents", "Dev"); +console.log(filePath); diff --git a/01-node-tutorial/answers/10-fs-sync.js b/01-node-tutorial/answers/10-fs-sync.js new file mode 100644 index 000000000..630d8f55e --- /dev/null +++ b/01-node-tutorial/answers/10-fs-sync.js @@ -0,0 +1,17 @@ +const { readFileSync, writeFileSync } = require("fs"); + +writeFileSync("./temporary/fileA.txt", "Once upon a time there was a king.\n", { + flag: "a", +}); +writeFileSync( + "./temporary/fileA.txt", + "The king liked to only eat steak and chicken.\n", + { flag: "a" } +); +writeFileSync("./temporary/fileA.txt", "The king died of Gout. The End.\n", { + flag: "a", +}); + +const fileContents = readFileSync("./temporary/fileA.txt", "utf8"); + +console.log(fileContents); diff --git a/01-node-tutorial/answers/11-fs-async.js b/01-node-tutorial/answers/11-fs-async.js new file mode 100644 index 000000000..6ee9f25db --- /dev/null +++ b/01-node-tutorial/answers/11-fs-async.js @@ -0,0 +1,36 @@ +const { writeFile } = require("fs"); +console.log("at start"); + +writeFile("./temporary/fileB.txt", "This is line 1\n", (err, result) => { + if (err) { + console.log("This error happened: ", err); + return; + } + console.log("line 1 completed"); + writeFile( + "./temporary/fileB.txt", + "This is line 2\n", + { flag: "a" }, + (err, result) => { + if (err) { + console.log("This error happened: ", err); + return; + } + console.log("line 2 completed"); + writeFile( + "./temporary/fileB.txt", + "This is line 3\n", + { flag: "a" }, + (err, result) => { + if (err) { + console.log("This error happened: ", err); + return; + } + console.log("line 3 completed"); + } + ); + } + ); +}); +// appears as second line because writeFile is async, rest of the script keeps running +console.log("at end"); diff --git a/01-node-tutorial/answers/12-http.js b/01-node-tutorial/answers/12-http.js new file mode 100644 index 000000000..8d0be9141 --- /dev/null +++ b/01-node-tutorial/answers/12-http.js @@ -0,0 +1,19 @@ +const http = require("http"); + +const server = http.createServer((req, res) => { + if (req.url === "/") { + res.end(` + Hello Server! :) + `); + } else if (req.url === "/about") { + res.end(` + About Page + `); + } else { + res.end(` + 404 - page not found :( + `); + } +}); + +server.listen(3000); diff --git a/01-node-tutorial/answers/prompter.js b/01-node-tutorial/answers/prompter.js index 38e93d831..bce6126af 100644 --- a/01-node-tutorial/answers/prompter.js +++ b/01-node-tutorial/answers/prompter.js @@ -22,7 +22,7 @@ const getBody = (req, callback) => { // here, you could declare one or more variables to store what comes back from the form. let item = "Enter something below."; - +const salt = "-_-CTD"; // here, you can change the form below to modify the input fields and what is displayed. // This is just ordinary html with string interpolation. const form = () => { @@ -45,7 +45,18 @@ const server = http.createServer((req, res) => { console.log("The body of the post is ", body); // here, you can add your own logic if (body["item"]) { - item = body["item"]; + // capitalize first letter of every word entered, and add SALT for a cool hash! + const wordsArr = body["item"].split("+"); + console.log(wordsArr); + const capitalizedWords = wordsArr.map((word) => { + if (word.length > 0) { + return word.charAt(0).toUpperCase() + word.slice(1); + } + return ""; + }); + item = capitalizedWords.join("-_-") + salt; + + console.log(item); } else { item = "Nothing was entered."; }