DEV Community

Zhangwuji
Zhangwuji

Posted on

study from node.js

give your path-string; how do you determine that it exist?

path.dirname(path)
Enter fullscreen mode Exit fullscreen mode

will return a directory path of it;
fs.existsSync(path) will return a valueOf boolean;

 fs.mkdirSync(directoryPath, { recursive: true });
Enter fullscreen mode Exit fullscreen mode

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();
                });
Enter fullscreen mode Exit fullscreen mode

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;

Top comments (0)