在使用同步写一次
"/dir": (request, response) => { response.writeHead(200, { "Content-type": "text/html;charset=utf-8" }); ["图片", "文件", "书籍", "视频"].forEach(async item => { fs.mkdirSync(path.join(__dirname, `/${item}`)); fs.writeFileSync( path.join(__dirname, `/${item}/${item}.txt`), "我还是要向世界发出hello world" ); console.log("我在里面"); }); console.log("我在外面"); response.end(); },然后控制台输出是"我在里面,我在里面,我在里面,我在里面,我在外面",async/await 的同步终究是个异步,只是在代码块中执行像同步
空文件夹直接删除
"/clear": async (request, response) => { response.writeHead(200, { "Content-type": "text/html;charset=utf-8" }); ["图片", "文件", "书籍", "视频"].forEach(async item => { await rmdir(path.join(__dirname, `/${item}`)); }); response.end(); },不为空的文件夹是无法直接删除的,删除的是文件而不是文件夹也会报错.
DocsNode.js fs 文档
nodejs-fs