当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Removing directory contents in node works only on server restart

问题描述:

I've got an app written in node and I want to remove single directory contents everytime user hits GET request on homepage ("/"). The problem is that it works ok after initial server start and reaching the homepage, but all consequent tries (like refreshing) don't trigger 'content removal' process (console.log(files) from the example below works fine).

In this case, directory gets empty only after shutting down the node server.

To achieve my goal I'm checking directory content with fs.readdir and if it's there I'm using 'emptyDir' method ( fs-extra package) to delete files.

Code:

app.get('/', function (req, res) {

res.render('home', { error: req.flash("error") });

fs.readdir(__dirname + "/assets", function (err, files) {

if (err) {

console.log(err);

} else {

if (files.length > 0) {

console.log(files); //debug

fs.emptyDir(__dirname + "/assets");

}

}

})

});

Thanks for help in advance! Cheers!

分享给朋友:
您可能感兴趣的文章:
随机阅读: