A number of methods to repeat information in Node.js
In Node.js, there are a number of methods to repeat information., let’s check out the attainable methods and evaluation every of them. That is my forty fourth Medium article.
The copyFile()
perform, which may copy a file on to the vacation spot listing, performs the best motion.
fs.copyFile('./knowledge.txt', './dest/data.txt');
The above technique, asynchronously copies the file from src to dest. If dest is already exists then by default it’s overwritten. There aren’t any args handed to the callback perform over than any attainable exception. Node.js doesn’t be certain that copy operations are atomic. Node.js will try to delete the goal file if an error occurs after opening the goal file for writing.
There’s a drawback once we use the above technique. If the goal listing doesn’t exist then an exception will likely be thrown as a result of the goal listing should exist (the tactic won’t routinely create the goal listing). Due to this fact, earlier than utilizing the above technique, consumer should validate whether or not the goal listing definetly exists or not? If the goal listing doesn’t exists, consumer may use fs.mkdir()
or fs.mkdirSync()
to create the goal listing. copyFile()
technique can’t copy directories.
On this method, learn the content material of the supply file after which write to the goal file. If the content material of the supply file needs to be modified throughout copying, this technique is appropriate
The drawback of this technique is similar because the above copyFile()
technique. readFile()
technique is used to learn the contents of the supply file and writeFile()
technique can solely write information in current directories. By utilizing this technique we will’t copy directories. The content material will be modified whereas being copied is the benefit of utilizing this technique.
readFile()
technique and writeFile()
technique are the entire block of operation knowledge. If the file measurement is giant the above technique will put extra pressure on system sources. createReadStream()
and createWriteStream()
is to make use of the best way of stream to control knowledge.
fs.createReadStream('./knowledge.txt').pipe(fs.createWriteStream(`./data.txt`));
The brand new fs.cp()
technique has been added since model 16.7.0 of Node.js. By utilizing this technique, your complete listing construction together with subdirectories and information will be copied asynchronously from src to dest. fs.cp()
technique can copy both a file or a listing. The configuration’s recursive
property must be set to true if a listing copy is required.
To repeat information
To repeat the listing, together with subdirectories and information.
As you may see, this fs.cp()
technique is a lot better than the above 3 strategies.
- The dest listing doesn’t must be required to exist. The dest listing will likely be routinely created if it doesn’t exist already (whatever the stage of a listing)
- You may fully copy information in your complete folder, together with subdirectories, with out recursively copying them individually.
When you’ll use this technique very first thing first you might want to verify the Node.js model!
What if you wish to copy each file within the folder however you solely have a decrease model of Node.js? We are able to recursively copy some information along with the native cp command for Linux, which is roofed within the subsequent part:
Find out how to use:
copyDir('./part', './web page/dwelling');
To execute Linux native instructions, we might use the exec
or spawn
instructions in child_process
. To repeat information or directories, the cp command in Linux is used.
You may in a position to make use of the above 5 strategies in case you are utilizing the most recent node model. Utilizing the fs module within the node, I’ve shared the quickest methods to repeat a file/listing. We completely regarded on the asynchronous strategies that we accessed by the fs module of Node.js.
Join our free weekly e-newsletter. Comply with us on Twitter, LinkedIn, YouTube, and Discord.
Trying to scale your software program startup? Try Circuit.