- Node.js introduced a
non-blocking I/O
environment to extend this concept to file access, network calls and so on.
-
we use
callbacks
a lot innode
as we deal withevents
,requests
, .. and we don't know when they will be executed -
to escape from
callback-hell
we usePromises or Async/Await
-
instead use
__dirname
intemplate-literals
which indicate the place where thejs
file exists ->const tempOverview = fs.readFileSync(`${__dirname}/templates/template-overview.html`, 'utf-8');
-
To avoid using callbacks, we can use the
File System Promises API
which allows the asynchronous methods to return promises.import {promises as fsPromises} from fs; // or import {promises as fs} from fs;
-
databases are just files.
- With Databases the content is structured, can be relational, and indexed.
-
With File System, you can only control where you write to the file, and where you read from the file
- so File System is only good for simple data storage.
File System Flags are used for identifying read/write
operations available when opening a file.
-
.open()
- Used to open a file. Takes a filename and flag as arguments.const writeData = async () => { const myFile = await fsPromises.open('myfile.txt', a+); }
-
.write()
/.writeFile()
Takes data, and options as arguments. -
const writeData = async () => { const myFile = await fsPromises.open('myfile.txt', a+); await myFile.write('add text'); } // or const writeData = async () => { const myFile = await fsPromises.writeFile('myfile.txt', 'The added text'); }
-
.read()
- Used to read a file. The file must be opened first. requires the creation of abuffer
to do so. Takes a buffer and options as arguments.const readData = async () => { const buff = new Buffer.alloc(26); // 26 characters max const myFile = await fsPromises.open('myfile.txt', a+); await myFile.read(buff, 0, 26); console.log(myFile); }
-
.readFile()
- Used to read the entire contents of a file. Takes a path and options as arguments.const readData = async () => { const myFile = await fsPromises.readFile('myfile.txt', 'utf-8'); console.log(myFile); };
-
.rename()
- Used to rename or move a file. Takes the old file path and new file path as arguments. -
.mkdir()
- Used to make new directories. Takes a directory path as an argument. -
.unlink()
- Used to remove a file. Takes a file path as an argument. -
.rmdir()
- Used to remove an empty directory. Takes a directory path as an argument. -
For removing directories that contain files without needing to remove the files first, it's easiest to use a third-party module such as rimraf
- it's a good idea to
stream
large datasets so that they don't overwhelm te (cpu, Ram,..) - to read streams we use the
File System (fs)
: itemmits
a event called'data'
fs.createReadStream('kepler_data.csv').on('data', data => {
console.log(data);
});
- It connects
stream source
withstream distination
- in the
pipe
, we put the thing that deal with incoming data in the start of thepipe
, ex : parsing data functionality
fs.createReadStream('kepler_data.csv')
.pipe(parser) // do parsing on incoming data
.on('data', data => {
console.log(data);
});