Introduction to the Node API
By the end of this, developers should be able to:
- Write a Node script using the File System API.
- Fork and clone this repository. FAQ
- Create a new branch,
training, for your work.
- Checkout to the
- Install dependencies with
Somewhat valid equations:
How are the two environments similar? How do they differ?
Lab: Research the File System API
Take a few minutes to read the following API documentation for the Node File System module.
While you're reading, imagine how you'd use each function. Write some example code in your notebook. Try to explain what each function does in your own words, including what sorts of arguments it takes and each argument's type.
If you finish early, look for other interesting functions in the File System module.
fs.readFile(file[, options], callback)
fs.writeFile(file, data[, options], callback)
Demo: Simple Node Scripts
We'll start with a very simple Node script. The file
console. Normally, we would need a browser to execute this code. But if we run
the file with
node bin/hello-world.js, it executes the file outside of the
We'll frequently need to pass some input into the scripts that we write with
Node. Think about
atom myFolderToOpen, for example. It's not written in Node,
but it is essentially a script that accepts arguments from the command line. It
wouldn't be very useful if we couldn't tell it what to open! When we open files
or folders in atom we pass in a path to that file or folder as an argument.
Let's takea look at
bin/process-argv.js to get a sense of how arguments
work in Node.
A more advanced "Hello, World!" with the Node file system API.
Write a Node.js command line function. It will take each line in from
names.txt, and output "Hey " plus the name from each line one at a time to
the command line.
Lab: Write a Randomizer
Randomize the lines in a file.
- Accept an input file and an output file name
- Shuffle the contents of the input file
- Create a new file to store the shuffled contents
How do you shuffle an array?
A common approach is called the Fisher-Yates Shuffle (aka the Knuth Shuffle).
Install the knuth-shuffle package
npm install knuth-shuffle. Then use the
knuth-shuffle package to
complete this lab.
Run your code using:
node bin/randomizer.js data/names.txt data/random_names.txt
Stretch Lab: Saving User Input
Write a Node script that prompts the user to enter some text into their terminal, then writes that text to a file. You will need to create a bin file and a lib file. The script should accept a filepath as an argument. This is where the text will be saved. The script should not use process.argv to accept that text from the user. Instead, you should research how to prompt the user for input in Node.js.
- Docs | Node.js
- Awesome Node.js packages
- Fisher-Yates shuffle, aka Knuth shuffle.
- Callback Hell
- All content is licensed under a CCBYNCSA 4.0 license.
- All software code is licensed under GNU GPLv3. For commercial use or alternative licensing, please contact firstname.lastname@example.org.