Node.js FS Module (Async): Complete Guide to Write, Read, Update, and Delete Files

Thapa Technical
63 followers
1 Video View·Dec 1, 2024
Hello what's up guys today in this video
we will see a synchronous AA module in our nut
js application yes guy so yesterday's
video was very awesome we
understood the FS module in great detail right and
we saw how we can work with it
Can perform the operation So guys today
we will again perform the curd operation but in an
asynchronous way so you can see
guys we have a fs1 file if anything If you
want to read then we have a read file. If
you want to update anything then for that we
have an append file. And if you want to
delete anything then for that we have a FS.
Unlink. We have used the same methods in yesterday's video. Had
used but one difference was
that a post fix was added at the end of each method
and that is write
file sync read file sync append file sync
unlink sync but today that is not there. Is the
code that we are going to do today, just that
we have to remove the sync and write this? It is
not so, its syntax is totally
different and that's
it, you should see it once and
I think that once you
If you see the code in the right file once, then the
remaining code, you
can do it by yourself, I am so confident that you
can do it, so you will understand that this is a task as a challenge for you. You
can say
that I will share all the definitions code or whatever
syntax there is, but you have to
do these three things yourself, but first you
watch it carefully, okay, so before that, guys,
please like the video and share it widely with your friends. If you have
n't subscribed to the channel,
please do so and hit the bell icon on the right side
so that you don't
miss the notifications of my awesome videos.
I make one video a day and that
too in the evening. Check it out at 4:00 pm guys,
I will be very happy, it will help you a lot,
right, so let's go
to our BS code editor, we
saw this in yesterday's video, but still no problem,
I am back, whatever. I am copying everything,
right? I create a new file
inside our FS folder
and write 'FS Normal' or '
FS A Sync'. 'Okay,
perfect, oh my God.' In yesterday's video
I had written JS one, I am
really sorry, I am not able to remember,
I have written J6 but it is a JS only, there is
no problem, both are of the same type, it is
all right, so I pasted it like this.
Look, I already
told you this in yesterday's video, no problem,
I will rename it and I would
love it guys, if you
do JSS for a second on this file, then
I am fine and perfect. Now there is no problem,
right, so you can see like
FS, we need to get the FS module, then
I have given the path and then I have said here
that oh my God, I did not sync yesterday,
no, it is perfect. So today we
will create a new file whose name will be
FS A Sync because today we are
working in an asynchronous manner, right means non
blocking, all the work will run one by one smoothly, there is
no tension at the end of the file. The
path again is the same, so we have
seen this much in yesterday's video, no no worries
but how much difference is there in the syntax, so you
can see what was in yesterday's video, the
path data of write file sync fs3 file file and then we have
this. Option so basically there
were three arguments but here today if you look so
it's a write file from here the one sync
word has disappeared but path data
options
path data options But here there is an extra
call back and this extra call
back is very important,
basically it helps you a lot in dealing with your errors,
everything else is the same, there is
no problem, on which path you have to
add whatever data you have. In which format to
add and if there is some error then
you can display it like, you
can tell that brother, this error has happened here,
we cannot read write basically. Ca
n't write ok so it's easy now let's see
how to do it so it's FS write and then we
have this
file is simple so first of all
give the path of the file this is your file path now
what data do you have to add inside it It
is the same data, this is the
initial commit, it comes to write again and again, I do
n't know why it is there, but no one,
I am adding UTF here and then,
now this It is important that now here we
need to pass a call back function. In
this way
you can also pass a normal traditional function
but I am using the fat arrow function.
Okay now here. You don't
get any error. What do you get here? You
get an error and you can write the condition here.
If there is an error then you
can log in or Kasol. Why do you have to do dlog, it is an error
right? Kasol dlog, I mean dot log,
I am saying kasol d error and then you
can throw error in this way and now
obviously you have to see on the console whether the
data has been added or not. If you
want something then I can know it, now
you can write a simple code for that, else
console.log file has been saved, simple, ok
now fill it again. Look carefully, here
currently there is only one file,
here at present we have only one
file, but now after running this code,
we have total two, it is fine, so let me clear it,
here I go. I have to change the name of the file
and that's I guess
FS or and boom guys this file has been
saved and I can see something also I can
see an extra here on FS
sync aa That's it, if I leave and
boom guys, look like this, our work is done,
okay, today we will not waste much time
because we had
understood it yesterday, but now let's move on to our next
pay and that's it, let's go now. It is written,
now I have to read it, I have to read it,
okay, so I just copied it,
I am pasting it, how to do it if
you want to read the data of the file, then we have
this particular method.
Called read and then file, everything is the same, it will
read the content asynchronously, there is
no problem, yes, it is important for us to see
what is there, brother, if there is any
change, then it is very clear, you
can see the change read and write here.
And that's your path, we have a path but the
data is not here because the data is not given to you,
you have to read the data, you will
get the data from here, okay and then this is the call
back, this is very This is important here
because here you can see clearly, so
now this is the call back guys, inside this we have to
pass two parameters, one is your error,
when we were writing, we had just
written the error, but now at the time of read, we have to
Right is to read the data, basically its job is to
read this data, then where will we get that data,
then you will get it from here,
from this particular data, see how
I have placed the right one here only.
Off write file I will go with read file rest
this file path will remain as it is this is the content
I am going to get Yeti 8 Yeti 8 I
told in yesterday's video if you do not write then
guys you will get the buffer value then
okay and we Have this error, now we need to
add one more parameter and that is your data, now
this is the data, you can add it here,
here I keep the data, okay this
Alright let's check it out here, let's
clear it, hit enter
and boom, look, this is the initial
data, how easy is it, of any content, which I
mean, any file, its content. Now how
easily can we read it? Isn't this awesome
right and yes if you don't write this na if
you don't
write this si what did you get see this buffer
value got and in yesterday's video I
told you oh here we Need to pass the options
and that is your what
TA 8 or ASCII or Haxe or full of such
but either you can do one thing
dot and then you say brother
convert it into string and give it to me either you
pass the option or So you do this and again we are
in track and that is this is the initial
data so this is the way guys read the data but I guess
this is the way this is a
little That's it, I don't enjoy it, so
I will leave it as a comment
so that you don't face any problem because I do
n't want to share my code, and
here only I guys convert it into simple
UTF-I8 and put a comma.
And I don't need it. Okay,
I don't need it. Okay, I don't need it. This is
it. So this is my read work. So you
can see that the same thing to update
is append but sync. You don't have to use it, just
append file and it will add the data,
otherwise it will create the file and add it too,
so path data options and then call back okay,
so this kind of similar to what we did with write,
you are feeling Okay,
now if you have any doubt then let me know
in the comment section, figure it out yourself,
right file when we are doing and then
when we append file then in both
What are the differences let me know in the comment
section okay so this is what I said let's go
with your append file okay first of all the
path of the file is very important because it has to be
added in it now here by
default I have a special character basically Skip
character so that I get a new line
and instead of initial I will go with
updated data Okay right updated data
and one let's tell in
difference in right file guys whenever you If you go to write
then it will overwrite but here when you are
doing your then which is
existing at the end so this is what
I did yet 8 error end or this is it file has been
saved instead of
updated control save
is fine OK the file has been updated let's check it
out or not this is not where this one
went OK or see here we have this this is the
updated data and I have a It
was written for a new line so that's it it's
visible so guys we saw
how we can append it now at the
end we have to see how we
can delete it now it's not a big deal
right that's it Let me copy this
and see there is a simple method for that
and that is your unlink, you have to call this method then you
need to pass the path and
Then again a call back so no matter what here
you have to pass the call back.
In tomorrow's video I will tell you
how now there is no need of this call back.
We will go with promise and FS forward
promise. There is a module on doing this in tomorrow's
video, let's see this now,
you will see this in many places, this is also fine, so what
we will do is like unlink, so let's
unlink the data, don't read it to me.
Nor do I care in which format
because I have to fly it, end
or file has been deleted, you can write
in this way, file has been deleted,
simple unlink,
I have already made the path of the file which has to be deleted.
And if there is an error, then show the error, let it be,
show it, okay,
clear and boom, the C file has been
deleted. Now if I go here,
you will see. Now there is no more which
file was that test hai na aa test
txt.gz That's all for the video and I really hope
you would have liked the video very much,
gradually you are getting confidence
that how can you read the files.
How can I create, update
and then delete? In yesterday's video,
I told you about renaming
but I guess now you can do that yourself too,
so now we are in the next which will be tomorrow's video. That
is also very important because
there I will tell you how now you
can play with Promises and
dealing with Promises is very
important for you and if it is
one of the most
demanded topic in JavaScript then your dom.
And then this promise, this sink wait, this is very
event loop and all two things, so among them there is
promise also, so you
cannot miss this video at all, so see you
tomorrow. In the video but before that guys
present wala which is your comment a day 11
present day 12 present or day five
present whatever it is I felt very awesome so
guys please keep your love like this
and also like the video
and Our next target is 8 lakh
subscribers so if you have not subscribed yet then
please do so, I will meet you tomorrow
in the video till then guys. Thank You So Much
Take Care
[Music]
