Fs-tivus for the Rest of Us

What better way to bring in the holidays than by taking a look at Node’s File System (fs) module!

At a high level, Node fs lets us read, write, create, update, and remove files from our computer using JavaScript on the back end, similar to how you would use Ruby or Python.

To use File System, I imported the module:

const fs = require(‘fs’)

For this example, let’s say we have a JSON file named data.json with a list of Christmas movies. We want to add a new movie to the file and save it to a new file called updated.json that doesn’t exist yet.

Our data.json file looks like this:

{
  "movies": [
    {
      "name": "Christmas Vacation",
      "director": "Jeremiah Chechik",
      "year": "1989"
    },
    {
      "name": "Die Hard",
      "director": "John McTiernan",
      "year": "1988"
    }
  ]
}

And the object we want to add to the JSON looks like this:

let christmasStory = {
  "title": "A Christmas Story", 
  "director": "Bob Clark",
  "year": "1983"
}

First, I read the contents of the data.json file and saved it into a variable:

fs.readFile('data.json', 'utf8', (err, data) => {
  if (err) throw err
  let obj = JSON.parse(data)
})

I used fs to access the File System module. The first argument readFile() takes is the file we want to read followed by ‘utf8’ which we add otherwise we’ll get a bunch of strange numbers that aren’t converted into characters. Since readFile() is asynchronous, I added a callback function that fires once it’s finished grabbing the data. I added in some minimal error checking, and tapped into data which holds our JSON file data. Finally, since the data read from the file comes to us a string, I converted it to JSON using JSON.parse().

Next, I pushed the new movie, A Christmas Story, into the movies array in the JSON.

fs.readFile('data.json', 'utf8', (err, data) => {
  if (err) throw err
  let obj = JSON.parse(data)
  obj.movies.push(christmasStory)
})

When writing JSON to a file, we have to turn it into a big string, the way it came to us, otherwise the file doesn’t know what to do with the data. To do this, I used JSON.stringify() and saved it into a variable named updated:

fs.readFile('data.json', 'utf8', (err, data) => {
  if (err) throw err
  let obj = JSON.parse(data)
  obj.movies.push(christmasStory)
  let updated = JSON.stringify(obj, null, 2)
})

To finish, I used fs.writeFile() which takes the file to write to and then the data you want to write. WriteFile() will create the file if it doesn’t exist.

fs.readFile('data.json', 'utf8', (err, data) => {
  if (err) throw err
  let obj = JSON.parse(data)
  obj.movies.push(christmasStory)
  let updated = JSON.stringify(obj, null, 2)
  fs.writeFile('updated.json', updated)
})

The finished code looks like this. To run the code, go to the root of your folder and run node yourFileName. Once run, you’ll see the new updated.json file appear in the project’s root:

const fs = require('fs')

let christmasStory = {
  "title": "A Christmas Story", 
  "director": "Bob Clark",
  "year": "1983"
}

fs.readFile('data.json', 'utf8', (err, data) => {
  if (err) throw err;
  let obj = JSON.parse(data)
  obj.movies.push(christmasStory)
  let updated = JSON.stringify(obj, null, 2)
  fs.writeFile('updated.json', updated)
})

We only scratched the surface of fs, but as you can see, the module gives us endless possibilities for creating and adding data files in our applications.

The Practical Developer稿源:The Practical Developer (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 综合技术 » Fs-tivus for the Rest of Us

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录