Lab1: Node.js feature–copyFile

Copying file is the most common operation in real life.  In Node.js, there are two copy-file functions:

similar:copy file from source to destination, if the destination file exit, overwrite the destination file. When an error happens during writing to an opened file, the destination file may be removed by Node.js. Both functions have the ‘src’ and ‘dest’ parameters.

difference: fs.copyFileSync returns ‘undefined’ while fs.copyFile has a callback as a parameter. Also, the usage of them is different. CopyFileSync is often used at the more important scene in which the backup and the origin need to be synchronized in real-time.

example of fs.copyFile(src, dest[, flags], callback):

if (err) console.log('nothing happened. the err code is '+err.toString());
else console.log('the origin file has copied to the backup file');



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s