Bootcamp
Search…
2.4: Disk Reading

Introduction

Node.js enables us to read files on our hard drive.
Node allows us to read and write to files on our hard drive, also known as "disk" for historical reasons.

Basic Example

mystuff.txt
hello world
index.js
// Import readFile function from global fs module. fs stands for file system.
import { readFile } from 'fs';
​
console.log('done importing from fs');
​
// Define callback function to run after retrieving file contents in readFile
const handleFileRead = (error, content) => {
console.log('running inside of handleFileRead');
​
// Handle errors if any
if (error) {
console.log('read error', error);
return;
}
​
console.log('content', content);
};
​
console.log('about to call readFile');
​
// 2nd param 'utf8' specifies the file encoding.
// Read more about UTF8 here: https://en.wikipedia.org/wiki/UTF-8
readFile('mystuff.txt', 'utf8', handleFileRead);
​
console.log('done calling readFile');
  1. 1.
    This is a program that reads the contents of the file called mystuff.txt.
  2. 2.
    The most important aspect of this code is that it is asynchronous, just like click events, setTimeout, or setInterval.
  3. 3.
    What do you think the order of the console.logs will be?

fs Module

  1. 1.
    fs is the built-in Node.js library that interacts with the computer's file system, i.e. the operating system abstraction of the hard drive. See official fs documentation here.
  2. 2.
    readFile is one of the functions that fs exports. See the specific documentation for readFile here.

error Parameter

readFile passes the error parameter to its callback function so that we know when something went wrong during file read. We should always have conditional checking for error when file reading.

Node.js Asynchronous Actions

Node.js performs all interactions with computer hardware asynchronously. This is because accessing resources external to our program can take an unknown amount of time depending on how "far away" those resources are. Accessing a resource from 4G may literally take 1 million times longer than accessing a resource from RAM.
An analogy for how "far away" resources can be.
"Relative KMs" and "Relative Distance" columns below are analogies to help us understand how "far away" various computer resources are.
Hardware Component
What we use it for
Time
Relative KMs
Relative Distance
x Times Slower Than RAM
RAM Memory
Local operations such as var x = 0;,
console.log(x);.
0.0000001 seconds
1 km
Your office.
~
SSD Hard Drive
Databases, files on the computer.
0.00015 seconds
150,000 km
Halfway to the moon.
10³
LAN Network
Networked Database access from a computer nearby.
0.15 seconds
150,000,000 km
Twice the distance to Mars.
10⁸
4G Mobile Network
Downloading something on a mobile device. (30 mbps)
1 second
1,000,000,000 km
Not quite to Saturn.
10⁹
  1. 1.
    When we retrieve data from the local computer's hard drive, as in the code example above, we write a callback function that runs when the operating system finishes loading that data from the hard drive into RAM.
  2. 2.
    We use an asynchronous callback (that is more difficult to understand and more complicated to write than synchronous code) because we cannot be sure how long such an action will take, and we know that it will take a relatively large amount to time to run.
  3. 3.
    Many factors could affect the speed of this action, such as the size of the file, the hardware of the drive (SSD versus disk), if another app is also accessing the file, etc. The operating system manages all of this "traffic" under the hood.
  4. 4.
    With the asynchronous syntax above, our program can continue to run and perform other logic while it waits for the file contents to be ready.
  5. 5.
    The table above shows that in the best case it takes 1000 times longer to get something from the disk than a variable from RAM (where all program variables are stored).
  6. 6.
    We will see even larger time scales when we learn about how to send and retrieve data over the Internet.

Example: Splitting Multi-Line Files

is a hidden character that creates a new line in text files. We can detect that character and create an array of lines in our program.
mystuff.txt
hello world
bananas
apples
oranges
index.js
import { readFile } from 'fs';
​
const handleFileRead = (error, content) => {
// Split the content of our file by lines
const lines = content.split('\n');
​
// For each line, log the line number and the content of that line
for (let i = 0; i < lines.length; i += 1) {
console.log(`Line ${i + 1}: ${lines[i]}`);
}
};
​
readFile('mystuff.txt', 'utf8', handleFileRead);
Newline characters are a relatively complicated topic. For our purposes will work for everything we want to do. It will not work in 100% of situations. Read more about newlines here.

Exercise

  1. 1.
  2. 2.
    Repeat the above code.
  3. 3.
    Set the file with an absolute path.
  4. 4.
    What happens when we try to read a file that doesn't exist?
  5. 5.
    What happens when the name is a folder and not a file?
  6. 6.
    Instead of hard-coding the file name, retrieve it from a command line parameter like the following.
node index.js mystuff.txt
Copy link
On this page
Introduction
Basic Example
fs Module
error Parameter
Node.js Asynchronous Actions
Example: Splitting Multi-Line Files
Exercise