In this video we are going to learn about Reading & Writing Files on our local filesystem using NodeJS.
For this we will use fs package.
Lets see how can we read data from the file.
So Goto the project and create a text file.
Lets say file name is message.txt.
Inside the message.txt.
Just type here a message.
This is a text message from file.
Now goto the index.js file and require the fs package.

var fs = require(\"fs\");
fs.readFile(\"message.txt\", \"utf-8\", function(err, data) {

Lets check it.
So goto the command prompt and run the command.

node index.js

Now you can see the text message from file.

Now If you want to catch errors such as the file you are trying to reach isn't found then you can do so like this:.

fs.readFile(\"not-found.txt\", \"utf-8\", (err, data) => {

Now let change the file not which not exist and lets check.
Re-run the index.js file.
You can see the error message.
Now lets see how can write text in file.

var fs = require(\"fs\");
var data = \"New File Contents\";
fs.writeFile(\"temp.txt\", data, (err) => {
if (err) console.log(err);
console.log(\"Successfully Written to File.\");

Now lets re-run the index.js file.

node index.js

You can see here the successfully written to file.
So lets see the file and here you can see the text inside the message.txt.
So in this way you can Read & Write Files in node js.