4.1.1_learn_you_node - MartijnKeesmaat/dating-app GitHub Wiki

FS module

The fs module provides an API for interacting with the file system in a manner closely modeled around standard POSIX functions.

Buffer

Buffer objects are Node's way of efficiently representing arbitrary arrays of data, whether it be ascii, binary or some other format. Buffer objects can be converted to strings by simply calling the toString() method on them. e.g. var str = buf.toString().

Async file-reading

fs.readFile(filePath, 'utf8', (err, data) => {
  if (err) throw err;
  console.log(data);
});

Parameters:

  • String as file path
  • Char type
  • callback fn with error msg and the actual data

Read directory

fs.readdir(process.argv[2], function (err, list) {
  list.forEach(function (file) {
    if (path.extname(file) === '.' + process.argv[3]) console.log(file)
  })
})

workshopper/learnyounode