Learning Material
This lesson covers how to use the node package manager npm to install additional modules from the npm library. It also covers event processing and async patterns in node.
There are several introductory videos to watch. This link and this video cover the event loop, and are referred to as external resources in the main video. This video covers try/catch/throw error handling in JavaScript, which is critical when writing asynchronous code.
You continue watching this video. This week, you watch from 1:45:57 of the video to 3:40:46.
Important: At about 2:03 of the video, the instructor does some git operations. Do not do these. You do need to create the .gitignore file, but you definitely do not do a git init inside the answers directory. That would create a git repository nested within your existing repository, which is a mess. In general, in this and all future videos, igore any git operations the instructor performs. You already have the git repository you need.
Assignments
Coding Assignment
As the instructor creates node programs, you duplicate his work just as before. You create a file package. json. Be sure that you are in the answers directory when you do the npm init to create this file. You also create several other small programs. Be sure to put them inside the answers directory. The programs you create for this lesson are:
- 13-event-emitter.js
- 14-request-event.js
- 15-create-big-file.js
- 16-streams.js
- 17-http-stream.js
For week 1, you created files in the node-express-course/01-node-tutorial/answers directory. For week 2, you continue to do your work in that same directory. However, before you do your work for this week, you switch to a new git branch. While the week1 branch is active, use the command
git checkout -b week2
As you will be working on the node-express-course repository for some weeks, this is the way you separate your assignments for each week.
Additional Assignment 1
Within the answers directory, create two files, practice1.js and practice2.js. The practice2.js file should just have a single variable called sentence, initialized to a sentence of your choosing. Export this variable. In practice1.js, require the sentence variable from the practice2.js module. Require also the os and fs modules. Then, also in practice1.js, write code that will create a file, ./content/practice.txt. Use asynchronous write operations. Write first the sentence and then the os.userInfo().username to the file. Test practice1.js to make sure these are written to the file (they will typically appear on the same line.)
Additional Assignment 2
Within the answers directory, create a file practice3.js. Begin this file with this line:
fs = require('fs').promises;
Within practice3.js, create an async function called makeFile. This function should create the file ./content/practice2.txt, using asynchronous calls that return promises. These must all be within a try/catch block. Use await to get the results of each call. The first line, written without the append flag, should say “This is the first line.” and should end with a newline character. The next lines, each of which should end in a line end, should read “This is line 2”, then “This is line 3”, and so on up to 10. These should be written in a loop, with the append flag. The catch statement should log the error, if any, to the console. After the makeFile function declaration, add a program line that calls makeFile(). Test the practice3.js program to make sure it creates the file correctly.
Mindset Assignment
Follow this link to access your mindset assignment for the week: Mindset Curriculum: Growth Mindset
Submitting Your Work
When you run npm install, you create a node_modules directory. You do not want this to be uploaded to Github, because it is large. So, create a file called .gitignore in the answers directory, with the single line
/node_modules
This prevents git from adding the contents of the node_modules directory. Then do the following to submit your work:
git add -A
git commit -m "answers for lesson 2"
git push origin week2
Then go to your github repository — the one you created with a fork. Create a pull request. The target of the pull request should be the main branch of the repository you created. Once you have created the pull request, you submit your link to the pull request in the assignment submission form (link in next paragraph).
When you’ve completed your Coding Assignment and Mindset Assignment this week, submit all of your work using:
Alpaca class use: | Squibby 2.0 Assignment Submission Form |
Baboon and any 2023 classes use: | Homework Assignment Submission Form |