WDX-180
Web Development X
Week 19 | Introduction to Node.js
Week 19 - Day 1 | Introduction to Node.js
Schedule
- Watch the lectures
- Study the suggested material
- Practice on the topics and share your questions
Study Plan
Your instructor will share the video lectures with you.
You can find the lecture code here
You can find the slides here
Lecture Notes
-
Node.js vs Browser Environment: The lecture highlights the differences between running JavaScript code in Node.js and a browser environment.
-
Window and Global Objects: The instructor explains how to use the
window
andglobal
objects to distinguish between Node.js and browser environments. -
Type of Operator: The lecture covers how the
type of
operator returns a string, and it’s always lowercase, so you should compare with a lowercase string. -
Runkit Environment: The instructor shows how to use the Runkit environment to test JavaScript code online and see the output in a nice HTML format.
-
npm Packages: The lecture touches on npm packages and how some can be run on both Node.js and browser environments, while others are specific to one or the other.
-
Use
window
to check if you’re in a browser environment -
Use
global
to check if you’re in a Node.js environment -
Be careful when comparing strings with the
type of
operator, as it always returns a lowercase string. -
Some npm packages can be run on both Node.js and browser environments.
-
window.document === document
-
Ctrl+L => Clear the console (both Node+Browser)
-
When converting bytes to megabytes or gigabytes, use 1024 instead of 1000 to avoid incorrect conversions.
-
Use the
process.argv
array to access command-line arguments in Node.js. -
Be careful when using the
fs
module, as it can delete files on your system.
Code Snippets:
- Checking the runtime environment:
if ( typeof window === "undefined" ){ // switch to global
console.log("Should be run in the Browser");
}
// Only runs in Node:
try {
global; // switch to windows for code that runs in the browser
console.log("Hello World!", 5 * 2 );
} catch(error){
console.log( "Sorry, only runs in Node");
}
References & Resources:
- Introduction to Node.js with Ryan Dahl
- Node.js at PayPal
- Run Node.js as a Service
- “Node. js versions get an even number, like 16.14. 0, while Current releases have an odd version number, like 17.5. 0”
- Free
Nodejs
hosting - Nodejs Builtin Modules
Summary
The first part of the lecture discusses the differences between running JavaScript code in a Node.js environment versus a browser environment. The instructor explains how to distinguish between these two environments using the window
and global
objects, and how to write code that is compatible with both.
The second part of the lecture covers various aspects of Node.js programming, including system interactions using the os
module, process management, and file operations:
-
Using the
os
module: The lecturer demonstrates how to use theos
module to get information about the system, such as total and free memory. They also show how to convert bytes to megabytes or gigabytes. -
Process management: The lecturer introduces the concept of process management in Node.js, showing how to access command-line arguments through the
process.argv
array. -
System interactions: Using the
os
module to get information about the system, such as memory usage. -
Process management: Accessing command-line arguments and managing processes in Node.js.
-
File operations: (Not covered in detail) Using the
fs
module for file system interactions.
Exercises
-
Explore the features of Nodejs (modules, REPL, etc.)
-
Complete the tasks found in the comments of the lecture code (search for the
TODO
keyword). - Install https://github.com/workshopper/learnyounode and complete the first 2 exercises (HELLO WORLD, BABY STEPS)
- Other tasks that you should practice on:
- Create an app that shows your systems total and free memory
- Create an app that accepts 2 numbers and adds them together
- Create an app that accepts 2 numbers and a command (add/sub/mul/div) and accordingly adds, subtracts, multiplies or divides those numbers. Make sure to move all the math operations in a separate module.
- Create an app that shows the time, e.g. node time.js
Note: Prefer GitBash in Windows for running node and other CLI tools
IMPORTANT: Make sure to complete all the tasks found in the daily Progress Sheet and update the sheet accordingly. Once you’ve updated the sheet, don’t forget to commit
and push
. The progress draft sheet for this day is: /user/week19/progress/progress.draft.w19.d01.csv
You should NEVER update the draft
sheets directly, but rather work on a copy of them according to the instructions found here.
Week 19 - Day 2 | Node.JS Practice
Schedule
- Study the suggested material
- Practice on the topics and share your questions
Study Plan
- Watch (or re-watch) yesterday’s lecture
- Complete all the coding challenges and exercises from Day 01
Week 19 - Day 3 | The FileSystem (fs) Module
Schedule
- Study the suggested material
- Practice on the topics and share your questions
Study Plan
-
Continue studying the Node.js introductory presentation from where we left off during our last lecture: The
fs
module: https://learn.js.org/learn/nodejs/introduction.html#/15 -
Make sure to read and practice building a basic web server using Node.js according to the official document_blank.
-
Install https://github.com/workshopper/learnyounode (if you haven’t done it already) and complete exercises 3, 4 and 5 (MY FIRST I/O, MY FIRST ASYNC I/O, FILTERED LS)
Exercises
- Create a Nodejs app that displays a list of programming languages along with information about them (creator(s), date of birth, etc.)
node lang.js
=> be creative with the output 🙂
- Create a Todo App using Nodejs:
- You should be able to run the app from the command line:
- node todo.js “Buy milk”
- node todo.js “Go to the gym”
- The app should then store the Task (“Buy milk”) along with the date in a JSON file (todo.json)
- When you run node todo.js (without any arguments) you should get a list of the todo tasks that are stored in the JSON file
- You should be able to run the app from the command line:
IMPORTANT: Make sure to complete all the tasks found in the daily Progress Sheet and update the sheet accordingly. Once you’ve updated the sheet, don’t forget to commit
and push
. The progress draft sheet for this day is: /user/week19/progress/progress.draft.w19.d03.csv
You should NEVER update the draft
sheets directly, but rather work on a copy of them according to the instructions found here.
Week 19 - Day 4 | Node.JS Practice
Schedule
- Study the suggested material
- Practice on the topics and share your questions
Study Plan
It’s time to continue practicing your Node.js
skills using learnyounode
.
Make sure that you have the package installed, up and running via learnyounode
in the terminal. We suggest that you complete the following 4 steps:
- My first I/O
- My first async I/O
- Filtered LS
- Make it Modular
Today, we also suggest to focus on the fs
module and practice what you’ve learned yesterday.
Questions? Cool things that you’ve learned? Post them on Slack or in the comments section below.
Above all, enjoy!
Extra Resources
Have some extra time and energy to devote on JavaScript?
You can try completing a challenge from here: https://javascript30.com/
Week 19 - Day 5 | The HTTP Module
Schedule
- Watch the lectures
- Study the suggested material
- Practice on the topics and share your questions
Study Plan
Your instructor will share the video lectures with you. Here are the topics covered:
-
Part 1: Node.js Modules, exploring the
fs
module and the MAKE IT MODULE exercise from learnyounode (90min) -
Part 2: Node.js, the HTTP module,
localhost
and creating a web server (120min)
You can find the lecture diagrams and code here
Study and practice:
- Basic NodeJS web server
- Solve the rest of the learnyounode exercises
- Web server:
- Check what the request.path is all about?
- require the path module and try again?
- Exercise:
- Require the os module
- Or (simpler) get the total and free memory
- run the cpus() method to get info about your computer’s CPUs
- Create an HTML page that displays the memory/CPU info and response.end()
- inspiration: CPU info page
References & Resources:
- I/O => Input/Output
- typeof Operator (unary)
- Operators: unary, binary, ternary
- unary: OPERATOR operates on a single value (OPERAND) typeof VALUE
- binary: OPERATOR operates on two values (OPERANDS) value + value
- ternary: OPERATOR operates on three values (OPERANDS) value ? value1 : value2
- VSCode: (for Mac, Ctrl => Cmd)
- CTRL + SHIFT + L # to select all at once
- CTRL + D # to select one by one
- 2 ways of importing and exporting Modules in Nodejs
- module.exports + required() (CommonJS => NodeJS)
- export + import (ES Modules, ESM => Browser)
- These days, both the Browser and Nodejs decided to use the ESM
- Error-first Callback (the idiomatic convention)
- Node.js HTTP server:
- The request object (1st parameter) is all about the information coming from the Browser (web client) (request.url, etc.)
- The response object (2nd parameter) is all about the response the server is about to send back to the client (FE)
- HTTP Status Codes
- The technical term for ‘text/html’ is MIME type
- Treat the request object as a READ-ONLY value
Weekly feedback: Hey, it’s really important for us to know how your experience with the course has been so far, so don’t forget to fill in and submit your mandatory feedback form before the day ends. Thanks you!