B
B
brutaler2011-04-26 03:14:34
Backup
brutaler, 2011-04-26 03:14:34

Copying a directory in node.js?

What is the correct way to copy a directory on the server using node.js?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexey Zhurbitsky, 2011-04-26
@blo

as an option for *nix

var exec = require('child_process').exec;
exec('cp -r /path/to/dir1 /path/to/dir2', function(error, stdout, stderr){...})

M
mark_ablov, 2011-04-26
@mark_ablov

fs.readdir and for each file - fs.createReadStream / fs.createWriteStream / util.pump

A
Anton Korzunov, 2011-04-26
@kashey

1. run fs.readDir on the folder.
2. in the callback we go and say fs.stat to the response line
3. in the callback we look at the file or folder. If the folder - then 1. In any case, saves the list as a folder and the list of files
4.When everything is ready ...
5.create the necessary folders in the right place
6.read the files from the source and write to the destination, here you need to watch something the number of threads is not off scale for max-open-files(100-300)
7. you can also chmodize files with the data from point 2
All actions are asynchronous.
For unknown reasons, it works several orders of magnitude faster than rsync in the case of a full data transfer.

T
Timur Shemsedinov, 2013-11-19
@MarcusAurelius

npm install ncp

var ncp = require('ncp').ncp;

ncp(source, destination, function (err) {
    if (err) { return console.error(err); }
    console.log('done!');
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question