Nodejs Read All File in a Folder
This article assumes that you have a slight understanding of what Node.js is and have used it at least once or twice to understand the inner concepts behind this commodity.
If y'all don't know nonetheless, you tin use Promises on some of the cadre modules of Node.js.
Let's run into an instance by reading 1 or more than files and why you would want to use this API instead of the old Callback-style API.
Reading a file
Before we jump into that topic and explain why yous might want to use the newer Promise-based API, let'southward meet how we used to read a file using the old API.
$ bear on index.js
" use strict " ; const { readFile } = crave ( " fs " ); readFile ( " package.json " , ( fault , fileBuffer ) => { if ( error ) { console . error ( error . message ); procedure . get out ( 1 ); } const fileContent = fileBuffer . toString (); console . log ( fileContent ); });
$ node index.js output of your package.json or an error here
Pretty standard stuff here. We only read our packet.json file and handle any possible error by stopping the execution of our script.
What near reading multiple files then?
Reading files
Let's see how we can use the same callback-based API to read some more files in our scripts.
" use strict " ; const { readFile } = require ( " fs " ); const fileRead = ( path ) => { readFile ( path , ( error , fileBuffer ) => { if ( error ) { console . error ( error . message ); procedure . leave ( 1 ); } const fileContent = fileBuffer . toString (); console . log ( fileContent ); }); }; fileRead ( " packet.json " ); fileRead ( " README.medico " ); fileRead ( " alphabetize.js " );
Here nil really strange, and once again pretty bones stuff. We even used a function for reading multiple files.
But there is ane major issue with this code: it is out of control.
If you lot endeavor to read your files that way, you have no guarantee that 1 file will be read after another. You might say hey, this is the expected behavior considering Node.js is an asynchronous platform and yous will exist absolutely right.
But if nosotros want to take some more control and accept our files read i after the other, nosotros would have to use the same Callback-style API as readFile.
" use strict " ; const { readFile } = require ( " fs " ); const fileRead = ( path , callback = null ) => { readFile ( path , ( mistake , fileBuffer ) => { if ( error ) { panel . error ( error . message ); procedure . exit ( 1 ); } const fileContent = fileBuffer . toString (); console . log ( fileContent ); if ( callback ) { callback (); } }); }; fileRead ( " package.json " , () => { fileRead ( " README.md " , () => { fileRead ( " index.js " ); }); });
Now our code is fixed! Our files are read in the order we look them to be read.
But imagine now reading a hundred files. Nosotros would easily fall into what is chosen callback hell.
Just fright not, considering Promises are an answer to that problem!
Read a file (again)
This fourth dimension, we will endeavor to utilize the newer Promise-based API for reading a file.
" use strict " ; const { promises : { readFile }} = require ( " fs " ); readFile ( " index.js " ). then ( fileBuffer => { panel . log ( fileBuffer . toString ()); }). take hold of ( error => { console . error ( error . message ); process . exit ( i ); });
Alright, there it is! We are doing exactly the same matter every bit before: reading a file. Merely we used a Hope-based API to practice so.
One of the greatest advantages of that is that it scales. Oh man does information technology scale. Yous could be reading ii or one hundred files and y'all could be using a syntax that is hands maintainable any the file count.
Reading files (again)
Let'southward see how we tin can rewrite reading multiple files but this time using our Hope-based API.
" utilize strict " ; const { promises : { readFile }} = require ( " fs " ); Promise . all ([ readFile ( " bundle.json " ), readFile ( " README.physician " ), readFile ( " index.js " ) ]). then (([ packageJson , readme , indexjs ]) => { console . log ( packageJson . toString ()); console . log ( readme . toString ()); console . log ( indexjs . toString ()); }). catch ( mistake => { console . fault ( fault . bulletin ); process . exit ( 1 ); });
Here we used Promise.all to easily wait for all promises in our assortment to be resolved or rejected (when a file is missing).
This allows united states to use a nice and make clean API for when nosotros want to read multiple files. And we don't have to use callbacks to handle each file.
We can even re-social club the brandish of our files if we want to.
Ane might say that adding more and more than files tin brand the Promise.and so callback parameters grow in size. And that person would be totally right.
Let's see what we can do if nosotros desire to foresee a time to come where nosotros would need to read a hundred files.
Reading hundreds of files (somehow)
" use strict " ; const { promises : { readFile }} = require ( " fs " ); const files = [ " package.json " , " README.md " , " index.js " ]; Promise . all ( files . map ( file => { return readFile ( file ); })). then ( fileBuffers => { fileBuffers . forEach ( fileBuffer => { console . log ( fileBuffer . toString ()); }); }). catch ( error => { console . error ( mistake . message ); process . exit ( 1 ); });
That'southward it, really. What nosotros did is use an array to store our files. We and then mapped on each file and return an array of promises, just like earlier, and so we mapped over each one of our resolved file buffers to display them in the console.
This is all that is necessary to display one, 2, a hundred or a grand files to the console with the just need to add the needed files in the files assortment.
Bonus: GNU cat
Let'due south see what it takes to re-invent the cycle and create our own cat utility program.
For those of yous that are unknown to what it does, it but takes all of its arguments as a file and outputs their content.
Reminding yous something we did earlier? Yes. That's nearly what nosotros did.
#!/usr/bin/env node " employ strict " ; const { promises : { readFile }} = require ( " fs " ); const files = process . argv . slice ( 2 ); Promise . all ( files . map ( file => { return readFile ( file ); })). then ( fileBuffers => { fileBuffers . forEach ( fileBuffer => { console . log ( fileBuffer . toString ()); }); }). catch ( error => { console . error ( error . message ); process . leave ( 1 ); });
The but thing that inverse is that it is now using process.argv instead of our manually crafted file array.
This means that every file that is passed as an statement tin be used and will exist used to read its content.
The shebang (first line) is there to help our crush because we volition try to make information technology alloy into our surroundings. Shhhhhhh.
$ mv index.js cat $ chmod +x cat $ ./cat README.md alphabetize.js package.json [output truncated, but it worked!]
13 single-lines of lawmaking for a cat-clone with error treatment. Pretty cool, huh?
Conclusion
Nosotros saw what we used to utilize to read files using the old callback-based API in Node.js and the newer using a Promise-based API.
Then if you are using Node.js and are stuck maintaining former API using a callback-style of doing things, know that you can upgrade things and get to the next footstep with this Promise-based API. readFile is only one of the many utilities that are available in the newer API of Node.js.
If I were to give an opinion on that, I think that the Promise-based API looks way cooler and more maintainable than its predecessor. And it allows us to leverage all the Promise.prototype methods that help us deal with asynchronous instructions easier.
If nosotros go a little deeper in the analysis, reading files can be retention inefficient, particularly when using the readFile method. If you really need to read more than files that are too actually heavy (like encrypting videos) you should exist using createReadStream instead.
But so you will not exist using Promises anymore only Streams. And that's a whole new topic that I won't comprehend here (but maybe another day).
Source: https://dev.to/aminnairi/read-files-using-promises-in-node-js-1mg6
0 Response to "Nodejs Read All File in a Folder"
Post a Comment