Fs module in node js!!!
Fs module in node js
What is fs module? fs stands for file system. What is the use of this module?why is it so important? My journey when I started with nodejs, this was first step. So, let's answer the question what is the use of this module? This module as name suggests allows us to work with files.Using this module, we can
1. Create files
2. Read files
3 Append text in file.
4. Delete files
We can perform these operations in two versions
1. Synchronous
2 Asynchronous
Let's understand Synchronous & Asynchronous.
Now, let's take an example in a hotel, the waiter takes order of first customer & then gives it to the cook & after the order is served for the customer 1 ,the waiter moves to next customer. This is called Synchronous. The second operation will be performed only after the first operation is performed successfully.
You must have guessed what is asynchronous version. Consider our hotel example, the waiter will take the order of first customer, pass it to the cook and immediately he will move on to the next customer. This is aynchronous. So, this was a short intro about Synchronous & asynchronous. If you want to use fs module,you need to require it first.
const fs = require ("fs");
Now you are ready to play with files. We will first see synchronous version.
Το create new folder fs. mkdirSync("Folder Name ");
To create new file fs.writeFileSync (filename, "Text to be written in the file");
To append text in a file fs: append FileSync ("File Path", "Text to be appended");
To delete a file fs.unlinkSync("File Path");
To delete a folder fs.rmdirSync("Folder Name ");
To read file
fs.readFileSync("Filename ");
Now if you execute this line of code, you will not get the actual data instead you get buffer data. To avoid this you can give encoding as an extra parameter.
fs.readFileSync( "Filename", "uf-8");
This was Synchronous Version. I hope you understood this. I am providing the link of official nodejs documentation to know more about fs module .
Node js official documentation
Well explained
ReplyDeletequiet good
ReplyDeleteInformative content and good explanation.
ReplyDeletevery well done
ReplyDeletevery well explained
ReplyDeleteWell done dude!
ReplyDeleteWow great work👍👍
ReplyDeleteGreat
ReplyDeleteWell explained
ReplyDeletevery well explained
ReplyDeleteGreat
ReplyDelete