Home > Web Front-end > JS Tutorial > Methods and examples of file copying in Node.js_node.js

Methods and examples of file copying in Node.js_node.js

WBOY
Release: 2016-05-16 16:46:15
Original
1326 people have browsed it

Node.js itself does not provide an API for directly copying files. If you want to use Node.js to copy files or directories, you need to use other APIs to achieve it. To copy a single file, you can use readFile and writeFile directly, which is easier. If you are copying all the files in a directory, the directory may also contain subdirectories, then you need to use a more advanced API.

Stream

Streams are the way Node.js moves data. Streams in Node.js are readable/writable. Both HTTP and file system modules use streams. In the file system, when using streams to read files, a large file may not be read all at once, but will be read several times. Data events will be responded to when reading. After reading, you can operate on the read data. In the same way, when writing to a stream, just like when reading, large files are not written at once. This way of moving data is very efficient, especially for large files. Using streams is much faster than waiting for the entire large file to be read before operating the file.

Pipeline

If you want complete control when reading and writing streams, you can use data events. But for pure file copying, read streams and write streams can transmit data through pipes.

Practical application:

Copy code The code is as follows:

var fs = require( 'fs' ),
stat = fs.stat;

/*
* Copy all files in the directory including subdirectories
* @param{ String } Directory to be copied
* @param{ String } Copy to the specified directory
*/
var copy = function( src, dst ){
// Read the directory All files/directories of
fs.readdir( src, function( err, paths ){
if( err ){
throw err;
}

paths.forEach(function( path ){
var _src = src '/' path,
_dst = dst '/' path,
readable, writable;

stat( _src, function( err, st ){
if( err ){
throw err;
}

                                                                                                                                                                         Stream( _src );
                                                                                                                         Stream
                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                   since 🎜>                                                                                                                });
});
} ;

// Before copying the directory, you need to determine whether the directory exists. If it does not exist, you need to create the directory first
var exists = function( src, dst, callback ){
fs.exists( dst, function( exists ) {
                                                                                                                                   fs.mkdir( dst , function(){
                                                                                                                                     callback( src, dst );
//Copy directory
exists( './src', './build', copy );


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template