Junior — Middle
What methods or functions are used to read data from files via the FS module?
sobes.tech AI
Answer from AI
In Node.js, the fs module provides the following methods for reading data from files:
fs.readFile(path, [options], callback)— asynchronous file reading, returns content in the callback.fs.readFileSync(path, [options])— synchronous file reading, returns content directly.fs.createReadStream(path, [options])— creates a stream for reading the file in parts.
Example of asynchronous file reading:
const fs = require('fs');
fs.readFile('example.txt', 'utf8', (err, data) => {
if (err) {
console.error(err);
return;
}
console.log(data);
});