Delete several files in node.js

后端 未结 8 1437
自闭症患者
自闭症患者 2021-02-15 23:58

What is the best way to delete several files in node.js?

function deleteFiles(files, callback){
    ...
}

var files = [\'file1.js\', \'file2.jpg\', \'file3.css\         


        
8条回答
  •  不知归路
    2021-02-16 00:20

    Here is a code to delete several files from directory in node.js,

    var fs = require('fs');
    
    var removeFile= function (err) {
        if (err) {
            console.log("unlink failed", err);
        } else {
            console.log("file deleted");
        }
    }
    
    fs.unlink('filepath', removeFile); // for single file
    
    
    //for multiple files...
    
    _.map(yourArray,(files)=>{
        fs.unlink('filepath', removeFile);
    });
    

提交回复
热议问题