我的应用程序中有这条路线。它的扫描文件根据url参数在某些目录中命名。目前我的JSON看起来像;
[{"chapter":"642","paths":["00.png","01.png","02.png","03.png","04.png","05.png".....
路线:
app.get('/api/oku/:name', function (req, res) {
var files = fs.readdirSync('./public/manga/' + req.params.name);
var files = files.map(function(item){
var subfolders = fs.readdirSync('./public/manga/' + req.params.name + '/' + item);
return {
chapter: item,
paths: subfolders
}
})
res.json(files);
})
我只想为每张图片添加路径。我要添加的路径是;
"manga/" + req.params.name + "/" + item + "/"
所以简单地看起来就像manga/Naruto/542/00.png
答案 0 :(得分:2)
试试这个:
app.get('/api/oku/:name', function (req, res) {
var files = fs.readdirSync('./public/manga/' + req.params.name).map(function(item){
var subfolders = fs.readdirSync('./public/manga/' + req.params.name + '/' + item);
return {
chapter: item,
paths: subfolders.map(function (i) {
return "manga/" + req.params.name + "/" + i + "/";
}
}
})
res.json(files);
})
另外,不要两次声明文件变量。