Javascript 如何使用提取请求获取多个文件?

Javascript 如何使用提取请求获取多个文件?,javascript,node.js,vue.js,Javascript,Node.js,Vue.js,我向服务器发出了一个获取请求,该服务器向我发送了一个json文件。但我需要两个不同的文件。请告诉我如何实现这一点 ======================vue代码 mounted(){ fetch('/', { method: 'POST', // *GET, POST, PUT, DELETE, etc. mode: 'cors', // no-cors, *cors, same-origin headers: { 'Content

我向服务器发出了一个获取请求,该服务器向我发送了一个json文件。但我需要两个不同的文件。请告诉我如何实现这一点

======================vue代码

 mounted(){
    fetch('/', {
    method: 'POST', // *GET, POST, PUT, DELETE, etc.
    mode: 'cors', // no-cors, *cors, same-origin    
    headers: {
       'Content-Type': 'application/json',
        'Accept': 'application/json'
    },
   
  })
    .then(response => response.json())    
    .then(json => this.firstWindow = json)
    
    .then(json => console.log(json))
    .then(console.log(this.firstWindow))
    
  },

===========node.js code

router.post('/',(req, res) =>{  // request for sending file
    // console.log("POST");
    res.sendFile(path.resolve('./data/firstScreen.json'));
    res.sendFile(path.resolve('./data/secondScreen.json'));
})
在这里,我试图生成一个Json文件数组以发送到服务器。但在我把它写进fs后,它仍然是空的

=================== work with fs

 let dataOfScreens = []; 


let Mode = require('stat-mode'); 
let temp;  
fs.readdir(path, function(err, items) {   
    for (let i=0; i<items.length; i++) {        
        if(items[i].indexOf("System Volume Information")!= -1 || items[i].indexOf("hiberfil")!= -1 || items[i].indexOf("pagefile")!= -1 || items[i].indexOf("MSOCache")!= -1 || items[i].indexOf("swapfile")!= -1 || items[i].indexOf("Recovery")!= -1){
            continue;
        }                   
        fs.stat(path +  items[i], function(err, stats){
                if(err) throw err;
                let mode = new Mode(stats);
                if(mode.isDirectory()){
                   temp = "<папка>";
                } else{
                    temp = stats.size.toString() + " байт";
                }                
                firstJson.table.push({id:i, icon:i, fileName:items[i], sizeOrType: temp , dateOfChange: stats.mtime}); 
                dataOfScreens.push(firstJson);  // HERE !
                fs.writeFile('data/firstScreen.json', JSON.stringify(firstJson), (err)=>{
                    if(err) console.log("error");
                });

                secondJson.table.push({id:i, icon:i, fileName:items[i], sizeOrType: temp , dateOfChange: stats.mtime});
                dataOfScreens.push(secondJson);// HERE !
                fs.writeFile('data/secondScreen.json', JSON.stringify(secondJson), (err)=>{
                    if(err) console.log("error");
                });
                
        });
        
    }
    
});
dataOfScreens.push(JSON.parse(fs.readFileSync('data/firstScreen.json', 'utf8')));
dataOfScreens.push(JSON.parse(fs.readFileSync('data/secondScreen.json', 'utf8'))); // my solution 

console.log(dataOfScreens);

为什么相对URL(即同一域)需要
模式:“cors”
?此外,连接JSON内容将导致无效的JSON。。。您需要读取服务器上的每个JSON文件,对其进行解析,将其添加到数组中,并在将其发送到响应中之前对数组进行字符串化。我想我只是忘记了通过
的方式删除它。然后(console.log(this.firstWindow))
-这将在请求发生之前记录到控制台,因为您没有向该控制台传递函数。然后,您需要读取每个json文件。。。查看并将每个结果添加到一个数组中,然后JSON.stringify该数组并在响应中发送。我可以再问你一个问题吗,不是关于这个主题吗?我对问题中的代码做了一些修改