Skip to content

Instantly share code, notes, and snippets.

@ApsarasX
Last active September 2, 2017 12:42
Show Gist options
  • Save ApsarasX/2f67b2a47d02e3d50fc9739b4e97f974 to your computer and use it in GitHub Desktop.
Save ApsarasX/2f67b2a47d02e3d50fc9739b4e97f974 to your computer and use it in GitHub Desktop.
Node Learning
const fs = require('fs');
function copy(src, dst) {
//小文件拷贝
fs.writeFileSync(dst,fs.readFileSync(src));
//大文件拷贝
//fs.createReadStream(src).pipe(fs.createWriteStream(dst));
}
function main(argv) {
copy(argv[0], argv[1]);
}
main(process.argv.slice(2));
//实现大文件拷贝时候的stream的pipe操作
const fs = require('fs');
function pipe(rs,ws) {
rs.on('data',chunk=>{
!ws.write(chunk) && rs.pause();
});
rs.on('end',()=>{
ws.end();
});
ws.on('drain',()=>{
rs.resume();
});
}
pipe(fs.createReadStream('./app.js'), fs.createWriteStream('./app.js.bak'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment