Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/421.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用Node.js中的搜索模式搜索数千个文件夹_Javascript_Node.js_Directory - Fatal编程技术网

Javascript 使用Node.js中的搜索模式搜索数千个文件夹

Javascript 使用Node.js中的搜索模式搜索数千个文件夹,javascript,node.js,directory,Javascript,Node.js,Directory,那么,让我解释一下。我正在开发一个节点应用程序,它必须访问文件夹中的某些特定文件。但问题从这里开始;有一千多个这样的文件夹,它们以文本/html的形式存储着独特的数据,而且将来还会增长。所以,我决定做一个搜索部分,可以搜索文件夹。但我被困在这里了。我本来想把所有文件夹的名称列在一个数组中,然后处理它们,但我很快意识到这是个坏主意。超慢!结构: app.js public searchable folder-1 unqiue.html

那么,让我解释一下。我正在开发一个节点应用程序,它必须访问文件夹中的某些特定文件。但问题从这里开始;有一千多个这样的文件夹,它们以文本/html的形式存储着独特的数据,而且将来还会增长。所以,我决定做一个搜索部分,可以搜索文件夹。但我被困在这里了。我本来想把所有文件夹的名称列在一个数组中,然后处理它们,但我很快意识到这是个坏主意。超慢!结构:

app.js
public
    searchable
         folder-1 
             unqiue.html
         folder-2
             unique2.html
         folder-3
             unique3.html
         ... 

这种情况一直持续下去。我是Node.js的新手,正在努力使事情变得正确。那么,我有没有办法做到这一点呢。欢迎有任何新想法。提前感谢。

而不是保存文件夹数组并从数组中检索文件

使用
fs
library和
cache
it将每个文件的路径以根目录作为数组动态保存

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)