Skip to content
1 change: 1 addition & 0 deletions 01-node-tutorial/answers/content/first.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello this is first text file
1 change: 1 addition & 0 deletions 01-node-tutorial/answers/content/second.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello this is second text file
1 change: 1 addition & 0 deletions 01-node-tutorial/answers/content/subfolder/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test txt
13 changes: 8 additions & 5 deletions 03-task-manager/optionalArrayMethodsReviewExtraAssignment.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const object = {

// Every time we call `.behavior()`, the data (number) inside `object` is
// incremented by 1, so we print "OOP demo 1", "OOP demo 2", etc.

object.behavior()
object.behavior()
object.behavior()
Expand All @@ -61,11 +62,13 @@ object.behavior()
// - The callback should return a boolean. If the return value is true, the
// element becomes a member of the new array. If the return value is false,
// the element is filtered (removed).
const integers = [1, 2, 3, 4, 5];
// evenNumbers will be interger % 2 for each integer
const evenNumbers = integers.filter((integer) => {
return integer % 2 === 0
})

const integers = [1, 2, 3, 4, 5];
// evenNumbers will be interger % 2 for each integer
// '%' is the "modulo" operator. Here we are checking if `integer` divided by 2, leaves a remainder of 0, which is true for even numbers and false for odd numbers.
const evenNumbers = integers.filter((integer) => {
return integer % 2 === 0
})

// - Array.prototype.map
// - The callback recieves each item of the array. The return value is pushed
Expand Down
6 changes: 3 additions & 3 deletions lessons/ctd-node-assignment-1.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Your homework should include the following programs:
2. `02-globals.js`: This program should use the `console.log` function to write some globals to the screen. Set an environment variable with the following command in your command line terminal: `export MY_VAR="Hi there!"` The program should then use `console.log` to print out the values of `__dirname` (a Node global variable) and `process.env.MY_VAR` (`process` is also a global, and contains the environment variables you set in your terminal.) You could print out other globals as well ([Node documentation](https://nodejs.org/api/globals.html#global-objects) on available globals). For each of these programs, you invoke them with `node` to make sure they work.
3. For the next part, you will write multiple programs. `04-names.js`, `05-utils.js`, `06-alternative-flavor.js`, and `07-mind-grenade.js` are modules that you load, using require statements, from the `03-modules.js` file, which is the main program. Remember that you must give the path name in your require statement, for example:

```
```javascript
const names = require("./04-names.js");
```

Expand All @@ -38,7 +38,7 @@ const names = require("./04-names.js");

1. `08-os-module.js`: This should load the built-in `os` Node module and display some interesting information from the resulting object. As for all modules, you load a reference to it with a require statement, in this case

```
```javascript
const os = require("os");
```

Expand All @@ -57,7 +57,7 @@ C:\Users\JohnSmith\node-express-course\01-node-tutorial\answers
1. `10-fs-sync.js`: This should load `writeFileSync` and `readFileSync` functions from the `fs` module. Then you will use `writeFileSync` to write 3 lines to a file, `./temporary/fileA.txt`, using the `"append"` flag for each line after the first one. Then use `readFileSync` to read the file, and log the contents to the console. Be sure you create the file in the `temporary` directory. That will ensure that it isn’t pushed to Github when you submit your answers (because that file has been added to the `.gitignore` file for you already which tells git not to look at those files).
2. `11-fs-async.js`: This should load the `fs` module, and use the asynchronous function `writeFile` to write 3 lines to a file, `./temporary/fileB.txt`. Now, be careful here! This is our first use of **asynchronous functions** in this class, but we are going to use them a lot! First, you need to use the `"append"` flag for all but the first line. Second, each time you write a line to the file, you need to have a callback, because the `writeFile` operation is asynchronous. Third, for each line you write, you need to do the write for the line that follows it within the callback – otherwise the operations won’t happen in order. Put `console.log` statements at various points in your code to tell you when each step completes. Then run the code. Do the console log statements appear in the order you expect? Run the program several times and verify that the file is created correctly. Here is how you might start:

```
```javascript
const { writeFile } = require("fs");
console.log("at start");
writeFile("./temporary/output.txt", "This is line 1\n", (err, result) => {
Expand Down
36 changes: 18 additions & 18 deletions lessons/ctd-node-assignment-11.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,15 @@ This front end uses a single-page style. There are multiple views in the page, i

Edit `app.js`. Comment out the following lines:

```
```javascript
app.get("/", (req, res) => {
res.send('<h1>Jobs API</h1><a href="/api-docs">Documentation</a>');
});
```

Add the following line below these commented out lines:

```
```javascript
app.use(express.static("public"));
```

Expand All @@ -134,7 +134,7 @@ To begin, add the following line to index.html, right above the close of the bod

These modules call one another using the exports that each provides. For this to work, you must declare it as type `module`. Create `index.js` in the public directory. The `index.js` module should read as follows:

```
```javascript
let activeDiv = null;
export const setDiv = (newDiv) => {
if (newDiv != activeDiv) {
Expand Down Expand Up @@ -203,7 +203,7 @@ You will need to create `loginRegister.js`, `register.js`, `login.js`, `jobs.js`

The `loginRegister.js` module is as follows:

```
```javascript
import { inputEnabled, setDiv } from "./index.js";
import { showLogin } from "./login.js";
import { showRegister } from "./register.js";
Expand Down Expand Up @@ -237,7 +237,7 @@ A separate function handles display of the div. (React works in similar fashion,

The `register.js` module is as follows:

```
```javascript
import {
inputEnabled,
setDiv,
Expand Down Expand Up @@ -285,7 +285,7 @@ export const showRegister = () => {

The `login.js` module is as follows:

```
```javascript
import {
inputEnabled,
setDiv,
Expand Down Expand Up @@ -328,7 +328,7 @@ export const showLogin = () => {

The `jobs.js` module is as follows:

```
```javascript
import {
inputEnabled,
setDiv,
Expand Down Expand Up @@ -369,7 +369,7 @@ export const showJobs = async () => {

The `addEdit.js` module is as follows:

```
```javascript
import { enableInput, inputEnabled, message, setDiv, token } from "./index.js";
import { showJobs } from "./jobs.js";

Expand Down Expand Up @@ -416,7 +416,7 @@ First, we’ll make register and logon work. For either register or logon, if th

Adding these capabilities to `register.js` gives the following:

```
```javascript
import {
inputEnabled,
setDiv,
Expand Down Expand Up @@ -514,7 +514,7 @@ Notice that we always clear out the input values before we switch to another pag

The `login.js` module becomes:

```
```javascript
import {
inputEnabled,
setDiv,
Expand Down Expand Up @@ -590,7 +590,7 @@ export const showLogin = () => {

Make these changes and test the application again. You should find that you can register and logon. Logoff doesn’t work right at present, but this can be corrected in `jobs.js` with the following change:

```
```javascript
} else if (e.target === logoff) {
setToken(null);

Expand All @@ -606,7 +606,7 @@ Note that logoff involves no communication with the back end. The user is logged

Next we need to make the changes so that we can create job entries. The `addEdit.js` module is changed as follows:

```
```javascript
addEditDiv.addEventListener("click", async (e) => {
if (inputEnabled && e.target.nodeName === "BUTTON") {
if (e.target === addingJob) {
Expand Down Expand Up @@ -663,7 +663,7 @@ There is a somewhat tricky part to this. We want to have edit and delete buttons

It looks like this in `jobs.js`:

```
```javascript
export const showJobs = async () => {
try {
enableInput(false);
Expand Down Expand Up @@ -715,7 +715,7 @@ So, plug this code into `jobs.js` at the appropriate point, and then try the app

However, the edit and delete buttons don’t actually work. This is because the click handler in `jobs.js` isn’t set to look for them yet. We can add a section to the click handler to remedy this.

```
```javascript
} else if (e.target.classList.contains("editButton")) {
message.textContent = "";
showAddEdit(e.target.dataset.id);
Expand All @@ -726,7 +726,7 @@ The `dataset.id` contains the id of the entry to be edited. That is then passed

This function is in `addEdit.js`, and should be changed as follows:

```
```javascript
export const showAddEdit = async (jobId) => {
if (!jobId) {
company.value = "";
Expand Down Expand Up @@ -778,7 +778,7 @@ With this change, the `add/edit` div will be displayed with the appropriate valu

So far, so good, but what happens when the user clicks on the update button? In this case, we need to do a PATCH instead of a POST, and we need to include the id of the entry to be updated in the URL. So we need the following additional changes to addEdit.js:

```
```javascript
if (e.target === addingJob) {
enableInput(false);

Expand Down Expand Up @@ -837,13 +837,13 @@ You’ll call the jobs delete API, and in the URL you will include the ID of the

**Note:** There is an error in the implementation of the delete operation in the jobs controller. The instructor’s guidance is to use this line:

```
```javascript
res.status(StatusCodes.OK).send();
```

This is incorrect, because an empty body is not valid JSON. Change it to:

```
```javascript
res.status(StatusCodes.OK).json({ msg: "The entry was deleted." });
```

Expand Down
14 changes: 7 additions & 7 deletions lessons/ctd-node-assignment-12.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ You do not need a `public` directory. The pages are rendered by the `EJS` engine

Next, create the boilerplate `app.js`. It should look as follows:

```
```javascript
const express = require("express");
require("express-async-errors");

Expand Down Expand Up @@ -227,7 +227,7 @@ SESSION_SECRET=123lkawjg091u82378429

The secret is some hard to guess string — and you **_never_** want to publicize it to Github! Then, add the following lines to `app.js`. These lines should be added _before_ any of the lines that govern routes, such as the `app.get` and `app.post` statements:

```
```javascript
require("dotenv").config(); // to load the .env file into the process.env object
const session = require("express-session");
app.use(
Expand All @@ -241,7 +241,7 @@ app.use(

Change the logic so that the secret word is stored and retrieved in the session, as follows:

```
```javascript
// let secretWord = "syzygy"; <-- comment this out or remove this line
app.get("/secretWord", (req, res) => {
if (!req.session.secretWord) {
Expand All @@ -267,7 +267,7 @@ This is the key used to retrieve session data. You can also see that the `HttpOn

We want to store the session data in a durable way. To do this, we’ll use Mongo as a session store. Replace the one line that does the `app.use` for session with all of these lines:

```
```javascript
const MongoDBStore = require("connect-mongodb-session")(session);
const url = process.env.MONGO_URI;

Expand Down Expand Up @@ -312,7 +312,7 @@ app.use(require("connect-flash")());

We want to set some messages into flash. To do this, change the `POST` route for `/secretWord` to look like this:

```
```javascript
app.post("/secretWord", (req, res) => {
if (req.body.secretWord.toUpperCase()[0] == "P") {
req.flash("error", "That word won't work!");
Expand Down Expand Up @@ -344,7 +344,7 @@ Whoa! you may be saying. That doesn’t look like HTML! What will the browser do

But, the problem is that the `info` and `errors` arrays need to get passed into the EJS file, when the render is called. This could be done as follows:

```
```javascript
res.render("secretWord", {
secretWord,
errors: flash("error"),
Expand All @@ -354,7 +354,7 @@ res.render("secretWord", {

But this is a little clumsy, because if we have a bunch of pages we render, every render statement would have to be modified. So, instead, we put the values in `res.locals`. That hash contains values that are always available to the EJS rendering engine. As follows:

```
```javascript
app.get("/secretWord", (req, res) => {
if (!req.session.secretWord) {
req.session.secretWord = "syzygy";
Expand Down
Loading