Introduction to Node.js
This practical course offers an introduction to what is a Node module and goes into greater detail by working with one of Node's built-in modules. In addition, this course explains how Node's non-blocking I/O operations differ from blocking I/O operations.
- Understand what a module is
- Learn about the built-in FileSystem module
- Learn how to use the FileSystem readFile method to read a text file
- Learn how to use a blocking method in Node.js
- Understand the differences in the order of output between non-blocking and blocking methods
This course is intended for anyone who wants to learn about Node.js.
To get the most out of this course, you should have an interest in Node.js in general.
Introduction to Node.js - What is a node module and non-blocking I/O? A module is a file or multiple files that provide a self-contained block of code that could be included and used wherever needed. Node.js has built-in modules and node also allows for third party modules through the use of the node package manager known as npm. To demonstrate a module, I will use the built-in file system module and I will begin by typing 'const fs=require fs.' Next, I'm going to console.log(fs) for examination purposes. In the terminal, I will type 'node index.js' and hit 'Enter'. And there's a lot of information here. I'm going to scroll through and all this information from the console log are some of the features and methods that are part of the file system module.
For this video, I'm going to use the readFile method to read a file. And in order to read a file, I need to make a file first. I will call it note.txt and in the file, I will type 'This is a note to be read by the node filesystem readfile method.' And I'm going to transition to the documentation of the readFile method. Node has a very in-depth documentation system and on the screen I have the readFile method and I'm going to point out one difference in the documentation. Node.js, at the making of this video, supports two module systems, CommonJS and ES modules. For maximum compatibility, I'm using the CommonJS module system, which uses the required statement that I wrote earlier. This is in place of the import statement that's listed right here. Only recently has node fully supported ES modules. And that is where this import statement is derived from.
I'm going to use the readFile method almost as written. I need to add the encoding option in order to read the node file properly. Transitioning back to the lab environment, I will comment out the console log. I will type 'fs.readfile.' The first argument of the readfile method is the path of the file that is to be read. Since the file is local, I will just type 'node.txt'. The next argument will be an option and I will type 'utf8' for encoding. The last argument is a callback function. I will use an arrow function with two arguments; error for error and data for the file contents. And I will console log the data. In the terminal, I will run note on index.js again and the result is, This is a note to be read by the note file system readFile method. And this is a typical use case of the module system in Node.js.
Now, I'm intentionally using the file system module to demonstrate another feature of Node.js, known as non-blocking I/O. To demonstrate this, I will start by adding a console log with the text 'non-blocking'. I will make a new variable 'const sum = 35 + 5'. I will then console.log(sum). I will execute this file in the terminal and examine the order of the console log messages. First is the non-blocking text, then the sum, then the contents of the file. Why were the file contents read and console log last in the terminal? That is because the readFiled method is non-blocking. When the method is executed, it still continues onward to execute other code instead of blocking the system from further execution. By the time the contents of the note file is read, the other two console logs have already finished executing. To take this a step further, Node.js offers blocking versions of non-blocking methods.
On the screen, I have a new method, readFileSync, which is the blocking version of the readFile method. Looking at this documentation, you may have noticed that the readFileSync method does not take a callback function as a argument. I will transition back to the lab environment. I'm going to convert the readFile method to the readFileSync method. I will type 'const data' and set it equal to the current fs.readFile method. I will copy the console log data and place it below the method. I will now delete the console log and the callback function from the readFile method. I will change readFile to readFileSync, the blocking version of the readFile method. And below, I will execute the code in the terminal. Now, the text of the note file is displayed first, followed by the non-blocking message and the console log of the sum.
Farish has worked in the EdTech industry for over six years. He is passionate about teaching valuable coding skills to help individuals and enterprises succeed.
Previously, Farish worked at 2U Inc in two concurrent roles. Farish worked as an adjunct instructor for 2U’s full-stack boot camps at UCLA and UCR. Farish also worked as a curriculum engineer for multiple full-stack boot camp programs. As a curriculum engineer, Farish’s role was to create activities, projects, and lesson plans taught in the boot camps used by over 50 University partners. Along with these duties, Farish also created nearly 80 videos for the full-stack blended online program.
Before 2U, Farish worked at Codecademy for over four years, both as a content creator and part of the curriculum experience team.
Farish is an avid powerlifter, sushi lover, and occasional Funko collector.