You can use the fs.readdir
or fs.readdirSync
methods.
(您可以使用fs.readdir
或fs.readdirSync
方法。)
fs.readdir
(fs.readdir)
const testFolder = './tests/';
const fs = require('fs');
fs.readdir(testFolder, (err, files) => {
files.forEach(file => {
console.log(file);
});
});
fs.readdirSync
(fs.readdirSync)
const testFolder = './tests/';
const fs = require('fs');
fs.readdirSync(testFolder).forEach(file => {
console.log(file);
});
The difference between the two methods, is that the first one is asynchronous, so you have to provide a callback function that will be executed when the read process ends.
(两种方法的区别在于第一种方法是异步的,因此您必须提供一个在读取过程结束时执行的回调函数。)
The second is synchronous, it will return the file name array, but it will stop any further execution of your code until the read process ends.
(第二个是同步的,它将返回文件名数组,但它将停止进一步执行代码,直到读取过程结束。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…