study from node.js
Zhangwuji
Posted on May 4, 2024
give your path-string; how do you determine that it exist?
path.dirname(path)
will return a directory path of it;
fs.existsSync(path)
will return a valueOf boolean;
fs.mkdirSync(directoryPath, { recursive: true });
this code will recursively create the directory;
when we upload file const file = ctx.request.files.file;
fs.createReadStream(file.filepath);
file.filePath is a temporary path;it will be deletetd after we used it;
const reader = fs.createReadStream(file.filepath);
const writer = fs.createWriteStream(filePath);
reader.on('error', reject);
writer.on('error', reject);
writer.on('finish', () => {
console.log('文件写入完成');
resolve();
});
reader will create a input stream; it reads data from disk to memory;
writer will create a input stream; it reads data from disk to memory;
💖 💪 🙅 🚩
Zhangwuji
Posted on May 4, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.