pjkiefer / w1-file-system

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

File Interaction with Node

By the end of this lesson, you should be able to read, write, and update files with NodeJS.

Core Learning Objective

  • Use NodeJS APIs to interact with files and the web

Sub-Objectives

  • Define CRUD
  • Read from files
  • Overwrite files
  • Programmatically update files

Prereqs

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!

Instructions & Guiding Questions

  • Begin by renaming data/pets.sample.json to pets.json
  • Question: In this lesson we will be modifying the contents of the pets.json file. Why do you think that file appears inside of the .gitignore file?

  • Your Answer: It appears in the gitignore file so it's not pushed to the repository. It's a data source and not part of the code and doesn't need to be versioned.


  • 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: C - Create would create the file or overwrite and existing file. R - Read would read the contents of the file so the program can use those U - Update would add content to the file through appending the data to the file or reading the original file, adding data then rewriting the file D - Delete would remove the file from the file system


  • Consider the above and then look back through the fs module documentation.
  • Question: What methods represent each CRUD action?

  • Your Answer: C - fs.writeFile or fs.writeFileSync but there are other options as well. R - fs.readFile or fs.readfileSync U - fs.appendFile or fs.writeFile to recreate original file with new content (after reading it) D - fs.unlink


  • Question: What is the difference between these two methods?

  • Your Answer: fs.readFile runs asynchronously and needs a callback function. fs.readFileSync reads it synchronously and doesn't need a callback function.


  • Question: Describe the difference between these two methods.

  • Your Answer: fs.writeFile replaces the original file with a new copy. fs.appendFile will append data to the file


  • 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: You can read the file into a data structure or string and use replace, splice or concat to add new data.


  • In Node, you'll have access to a global variable called __dirname. Add the following to your index.js file.
    const path = require('path')
    const petsFile = path.join(__dirname, 'data', 'pets.json')
  • Question: Describe what is happening in the above code.

  • Your Answer: __dirname from node documentation - 'The directory name of the current module'. It pulls the current file path and concats the parameters into a new logical path. petsFile = 'D:\JS400\Class1\w1-file-system\data\pets.json on my local machine'.

Exercise

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 }

Resources

About


Languages

Language:JavaScript 100.0%