Modern Full-Stack Development



Download 5,64 Mb.
Pdf ko'rish
bet33/107
Sana06.08.2021
Hajmi5,64 Mb.
#140576
1   ...   29   30   31   32   33   34   35   36   ...   107
Bog'liq
Modern Full-Stack Development Using TypeScript, React, Node

 File System (fs)
The File System module is one of the more often-used modules available. It provides you 
an API for working with the local file system in a pattern that closely matches that of the 
standard POSIX functions.
To use this module, you’ll need to require() it:
const fs = require("fs");
This, in fact, is necessary for all but one of the modules described here (and it’s true 
for most of the modules you’ll find in the Node documentation). That’s what the string 
in parenthesis after each title is: it’s the value you need to require() (you can, of course, 
assign it to a variable of whatever name you wish, though I tend to make the variable 
name and the require() value the same).
Now that we have the module imported, what can we do with it? Well, here’s some of 
what I think are the handier bits of functionality this module offers:
•  fs.copyFile() – As you would guess, it allows you to copy a file. You 
supply to it the source file to copy, and the destination location where 
to put it and Node will send the request to the underlying operating 
system to perform. Note that as is the case with most of Node, this 
call will be asynchronous. Therefore, it allows you to pass it a callback 
function to execute once the copy completes. Alternatively, there is 
a fs.copyFileSync() method that is synchronous and so will block 
your code until the operation completes. This pattern, of passing 
a callback or offering a synchronous version of the function, is a 
pattern repeated in much of this module and even other modules 
when asynchronous operations come up, so keep that in mind as 
we proceed, though I won’t mention it again. To be clear, it’s not 
something you’ll find available in every case, but in many, perhaps 
even most.
Chapter 2   a Few More words: advanCed node and npM


31
•  fs.readFile() – Yep, it reads a file and passes the data from it to a 
function you supply, wherein you can do whatever you like with it. 
And yes, there is, of course, a matching fs.writeFile() method, 
with which you can do things like writing a JavaScript object that you 
JSON.stringify() to marshal into a string to a file.
•  fs.unlink() – If you want to delete a file, you actually want to 
“unlink” it in POSIX-speak, so this module offers an fs.unlink() 
method. Pass it the path to the file, and thy bidding will be done!  
(You can also remove symlinks with this.)
•  fs.mkdir() – Not only can you work with files with this module but 
you can work with directories too. The fs.mkdir() method allows 
you to create directories. I should also note at this point that all 
the methods so far also accept an object with various options. For 
example, this method allows you to pass a mode key in the options 
object to specify the permissions for the directory (although note 
that this is not supported on Windows). This is a typical pattern that 
repeats itself frequently in this module as well. To delete a directory
the fs.rmdir() method is provided.
•  fs.stat() – If you’re looking for information about a file or directory, 
things like its size, last access time, and when it was created, then 
fs.stat() is your friend. It returns an fs.Stats object, which is an 
object within the File System module, and it contains many pieces 
of information including the size of the file or directory (size) and 
when it was created (birthtime).
•  fs.readdir() – This method allows you to read in the contents of a 
directory given its path, and returns to you an array of filenames, or 
an array of Buffer objects containing the names, or an array of  
fs.Dirent objects, one per file (which you get depends on the options 
you pass in, with an array of string filenames being the default).
In later versions of Node, starting around v10, a new subsection of the File System 
module was introduced: the Promises API. Basically, it provides methods matching 
the fs methods seen here and most of the others this module offers, but that return 
Chapter 2   a Few More words: advanCed node and npM


32
Promises. So, if you prefer a Promise-based coding style (or async/await on top of that), 
then have a look at the section on that in the docs. Other than different names (e.g., fs.
fsPromises.copyFile() and fs.fsPromises.readFile()) and the obvious syntactic 
differences, they otherwise are mostly the same as the non-Promise versions.

Download 5,64 Mb.

Do'stlaringiz bilan baham:
1   ...   29   30   31   32   33   34   35   36   ...   107




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish