How to get data from fs.readFile?

Sometimes, we want to get data from fs.readFile.

In this article, we’ll look at how to get data from fs.readFile.

How to get data from fs.readFile?

To get data from fs.readFile, we can get it from the 2nd parameter of the callback.

For instance, we write

const fs = require('fs');

fs.readFile('./index.html', (err, data) => {
  if (err) {
    throw err;
  }

  console.log(data)
});

to call fs.readFile with the path of the file to read and a callback that has the read file.

We get the read file content from the data parameter.

Conclusion

To get data from fs.readFile, we can get it from the 2nd parameter of the callback.