By the end of this lesson, you should be able to read, write, and update files with NodeJS.
- Use NodeJS APIs to interact with files and the web
- Define CRUD
- Read from files
- Overwrite files
- Programmatically update files
Before starting this lesson, Fork & Clone this repository. Make sure that you can run npm start and that you get the following output:
Hello, Node!
- Begin by renaming
data/pets.sample.jsontopets.json
-
Question: In this lesson we will be modifying the contents of the
pets.jsonfile. Why do you think that file appears inside of the.gitignorefile? -
Your Answer:
- Take a look at the NodeJS documentation for the fs module. When looking at documentation like this it can be overwhelming to start but you likely know more than you think. Take a moment to find a few concepts you understand.
- One of the most common patterns we'll come across as web developers is the concept of CRUD. Take a moment to define what CRUD represents.
-
Question: Imagine you have a file called
classmates.txt. For each part of CRUD, describe how the action would interact with the file. -
Your Answer:
- Consider the above and then look back through the fs module documentation.
-
Question: What methods represent each CRUD action?
-
Your Answer:
- Take a look at the following two methods: fs.readFile() and fs.readFileSync().
-
Question: What is the difference between these two methods?
-
Your Answer:
- Take a look at fs.writeFile() and fs.appendFile().
-
Question: Describe the difference between these two methods.
-
Your Answer:
- Imagine you want to edit the middle of a file. You can use the fs module and the JavaScript language.
-
Question: How would you do so?
-
Your Answer:
- In Node, you'll have access to a global variable called
__dirname. Add the following to yourindex.jsfile.const path = require('path') const petsFile = path.join(__dirname, 'data', 'pets.json')
-
Question: Describe what is happening in the above code.
-
Your Answer:
You now have the knowledge needed to edit files on your file system. A note of caution: this means you have the ability to delete files programmatically from your machine. That can be pretty dangerous! In this exercise and class, we will never be deleting any files programmatically. If you ever find yourself in a situation where you do need to do so, be careful.
For the rest of the time we have, use the knowledge you've gained to build functions that will modify data inside of the pets.json file. You should be able to create new pets, read information about all your pets, update a pet's information, and delete a pet from your records.
Assuming your pets.json starts with one pet like so:
[
{
"name": "Meowser",
"kind": "cat",
"age": 3
}
]You should make the following functions and they should return the values that are commented out.
console.log(read())
// [ { name: 'Meowser', kind: 'cat', age: 3 } ]
console.log(create('Duchess', 'bird', 2))
// [
// { name: 'Meowser', kind: 'cat', age: 3 },
// { name: 'Duchess', kind: 'bird', age: 2 }
// ]
console.log(create('Duchess', 'bird', 2))
// "Duchess" is already a pet!
console.log(remove('Snoopy'))
// No pet found by the name of "Snoopy"
console.log(remove('Duchess'))
// { name: 'Duchess', kind: 'bird', age: 2 }
console.log(read())
// [ { name: 'Meowser', kind: 'cat', age: 3 } ]
console.log(update('Duchess', 'bird', 3))
// No pet found by the name of "Duchess"
console.log(update('Meowser', 'cat', 4))
// { name: 'Meowser', kind: 'cat', age: 4 }
console.log(read())
// [ { name: 'Meowser', kind: 'cat', age: 4 } ]
console.log(update('Meowser', 'cat', 3))
// { name: 'Meowser', kind: 'cat', age: 3 }