Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 01-node-tutorial/answers/01-intro.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("Hello World!!");
2 changes: 2 additions & 0 deletions 01-node-tutorial/answers/02-globals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
console.log(`Current Directory: ${__dirname}`);
console.log(`MY_VAR Global Variable: ${process.env.MY_VAR}`);
14 changes: 14 additions & 0 deletions 01-node-tutorial/answers/03-modules.js
Original file line number Diff line number Diff line change
@@ -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]);
}
8 changes: 8 additions & 0 deletions 01-node-tutorial/answers/04-names.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const names = {
dad: "Hank Hill",
son: "Bobby Hill",
wife: "Peggy Hill",
dog: "Lady Bird",
};

module.exports = names;
5 changes: 5 additions & 0 deletions 01-node-tutorial/answers/05-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function sayWazzup(name) {
console.log(`Hey ${name} Waaaazzzuuuuup`);
}

module.exports = sayWazzup;
8 changes: 8 additions & 0 deletions 01-node-tutorial/answers/06-alternative-flavor.js
Original file line number Diff line number Diff line change
@@ -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;
5 changes: 5 additions & 0 deletions 01-node-tutorial/answers/07-mind-grenade.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function printCurrentDir() {
console.log(`Currently in directory: ${__dirname}`);
}

printCurrentDir();
5 changes: 5 additions & 0 deletions 01-node-tutorial/answers/08-os-module.js
Original file line number Diff line number Diff line change
@@ -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()}`);
7 changes: 7 additions & 0 deletions 01-node-tutorial/answers/09-path-module.js
Original file line number Diff line number Diff line change
@@ -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);
17 changes: 17 additions & 0 deletions 01-node-tutorial/answers/10-fs-sync.js
Original file line number Diff line number Diff line change
@@ -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);
36 changes: 36 additions & 0 deletions 01-node-tutorial/answers/11-fs-async.js
Original file line number Diff line number Diff line change
@@ -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");
19 changes: 19 additions & 0 deletions 01-node-tutorial/answers/12-http.js
Original file line number Diff line number Diff line change
@@ -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);
15 changes: 13 additions & 2 deletions 01-node-tutorial/answers/prompter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand All @@ -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.";
}
Expand Down