[英]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.