所以让我解释一下。我正在开发一个Node应用程序,它需要访问文件夹中的一些特定文件。但是问题从这里开始:有一千多个这样的文件夹,它们以文本/html的形式存储着独特的数据,而且将来还会有更大的增长。所以,我决定做一个搜索部分,它可以搜索文件夹。但我被困在这里了。我有一个想法,列出数组中文件夹的所有名称,并对它们进行处理,但我很快意识到这是个坏主意。超慢!结构:
app.js
public
searchable
folder-1
unqiue.html
folder-2
unique2.html
folder-3
unique3.html
... 而且这件事还在继续。我是Node.js的新手,我正努力把事情做好。我有什么办法能做到这一点吗。任何新的想法都会受到赞赏。提前谢谢。
发布于 2020-08-29 06:29:07
而不是保存文件夹数组并从数组中检索文件。
使用fs库动态地将指向根的每个文件的路径保存为一个数组,并使用cache库。
const fs = require('fs');
let filePathArr = [];
getfilePathArray('src');
function getfilePathArray(path) {
fs.readdir(path, (err, files) => {
if (files) {
files.forEach(file => {
if (file.indexOf('.') !== -1) {
filePathArr.push(path + '/' + file);
} else {
getfilePathArray(path + '/' + file);
}
});
}
console.log(filePathArr);
});
}然后,可以从缓存的数组中搜索唯一的文件名unique.html。
const searchedPath = filePathArr.find(path=>path.indexOf('unique.html') !== -1)https://stackoverflow.com/questions/63643200
复制相似问题