简体   繁体   English

使用require-all从目录和子目录递归加载文件

[英]Recursively load files from directory and subdirectories with require-all

I have json files within a directory and subdirectory that I am wanting to match with specific criteria, however I am unaware as how to work with the subdirectories. 我在要与特定条件匹配的目录和子目录中有json文件,但是我不知道如何使用子目录。

I am using require-all to find the json files: 我正在使用require-all查找json文件:

const reqAll = require('require-all')({
  dirname: __dirname + '/resource',
  filter: /(.+)\.json$/,
  recursive: true
});

My file tree is as such: 我的文件树是这样的:

MyDir
- file1.json
- SubDir
-- file2.json

Printing reqAll will output: 打印reqAll将输出:

{ 
  file1: { 
    path: /first,
    body: ...some body
  },
  SubDir: { 
    file2: { 
      path: /second,
      body: ...some body
    } 
  } 
}

I was initially using the following filter to weed out my data, as I was originally not using subdirectories, but now it makes sense to. 最初,我最初使用以下过滤器清除数据,因为我最初没有使用子目录,但是现在有意义。

let source = Object.values(reqAll).filter(r => {
    return r.path === req.url;
}

where req.url is the url of the http request I am sending in. ie: localhost:8080/first , so that this will match with the file1 file I have in my directory. 其中req.url是我正在发送的http请求的URL。即: localhost:8080/first ,以便它与我目录中的file1文件匹配。

The problem is that when I submit localhost:8080/second , I do not get a response because I cannot match to file2 as this is within a subdir. 问题是,当我提交localhost:8080/second ,没有得到响应,因为我无法与file2匹配,因为它位于子目录中。 Also sending localhost:8080/SubDir/file2 does not work. 同时发送localhost:8080/SubDir/file2不起作用。

Is there a way in which I can get this to work? 有什么方法可以使我工作?

In a comment you've written: 在您写的评论中:

So, I will perform a HTTP GET on, localhost:8080/first and it should return me the body of the file1 object. 因此,我将在localhost:8080 / first上执行HTTP GET,它将返回我file1对象的主体。 This does in fact work for this endpoint. 实际上,这确实适用于此端点。 However, it is when I perform a HTTP GET on localhost:8080/second that I cannot get the body back. 但是,当我在localhost:8080 / second上执行HTTP GET时,无法获取主体。

To do that, you'll need a recursive search for the entry with that path , something along these lines (see comments): 为此,您将需要对具有该path的条目进行递归搜索,类似于以下几行(请参见注释):

 const repAll = { file1: { path: "/first" }, SubDir: { file2: { path: "/second" }, SubSubDir: { file3: { path: "/third" } } } }; const req = {}; function findEntry(data, path) { for (const value of Object.values(data)) { // Is this a leaf node or a container? if (value.path) { // Leaf, return it if it's a match if (value.path === path) { return value; } } else { // Container, look inside it recursively const entry = findEntry(value, path); if (entry) { return entry; } } } return undefined; // Just to be explicit } for (const url of ["/first", "/second", "/third", "fourth"]) { req.url = url; console.log(req.url + ":", findEntry(repAll, req.url)); } 
 .as-console-wrapper { max-height: 100% !important; } 

I added a second subdirectory to ensure the recursion keeps working, and an example of what you get back if you don't find a matching path. 我添加了第二个子目录以确保递归继续工作,并提供了一个示例(如果找不到匹配的路径),您将得到什么。

You could, of course, build a map by processing repAll once up front and then reuse the map, which would be faster than this linear search: 当然,您可以通过repAll处理repAll然后再使用地图来构建地图,这比线性搜索要快:

 const repAll = { file1: { path: "/first" }, SubDir: { file2: { path: "/second" }, SubSubDir: { file3: { path: "/third" } } } }; const byPath = new Map(); function indexEntries(map, data) { for (const value of Object.values(data)) { if (value) { // Leaf or container? if (value.path) { map.set(value.path, value); } else { indexEntries(map, value); } } } } indexEntries(byPath, repAll); const req = {}; for (const url of ["/first", "/second", "/third", "fourth"]) { req.url = url; console.log(req.url + ":", byPath.get(req.url)); } 
 .as-console-wrapper { max-height: 100% !important; } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不使用require语句的情况下使用汇总加载子目录中的所有文件 - How to load all files in a subdirectories using rollup without require statements 如何在没有require语句的情况下使用webpack加载目录中的所有文件 - How to load all files in a directory using webpack without require statements Browserify需要目录中的所有文件 - Browserify Require All Files in Directory HTML / Javascript:获取给定目录中所有文件和子目录的列表 - HTML/Javascript: Getting a List of all files and subdirectories in a given directory 在Google云端存储的特定目录中获取所有文件和子目录 - Getting all files and subdirectories inside a specific directory in Google cloud storage 如何require()目录中的所有文件并发送参数 - How to require() all files in a directory and send arguments 从JavaScript / HTML5中的目录加载所有文件 - Load all files from a directory in JavaScript/HTML5 使用 PHP 为目录中的所有 JavaScript 文件递归生成脚本标签 - Recursively generate script tags for all JavaScript files within a directory with PHP FileSystem API-从目录中递归列出文件 - FileSystem API - Recursively listing files from directory drag and drop NodeJS递归列出目录中的文件 - NodeJS recursively list files in directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM