Fill an array based on express response

I have the following .js get route server

app.get('/', function(req, res) { var url; var final_res = []; endpoints.forEach(function(url){ request(url, function(error,response,body){ if(!error && response.statusCode == 200){ final_res.push(url.url); console.log(url.url); }else{ res.send(err); console.log(err); } }); }); }); 

And this is my js client where I get this exact result with jQuery

 $(document).ready(function() { $.get('http://localhost:3000/', function(data) { console.log(data); $("#body").text(data); }); }); 

When I open my index.html, it displays the user interface correctly and inside my terminal, where I execute my server.js, it displays the URL correctly. What I cannot do is use my data that my jQuery gets to populate the table inside my html. My table will be populated with URLs that are extracted from my endpoints.

I have some background in nodejs but I can't wrap it.

0
source share
2 answers

Since you need to know when multiple queries are being executed, I suggest you switch to using the Prompt Query Library so that you can use promises to track all queries. This library also automatically checks statusCode. So you can do this:

 const rp = require('request-promise'); app.get('/', function(req, res) { Promise.all(endpoints.map(url => { return rp(url).then(r => { return url.url; }).catch(err => { // rather than error, just return null result return null; }) })).then(results => { // filter out null values, then send array as the response res.json(results.filter(item => item !== null)); }).catch(err => { console.log(err); res.sendStatus(500); }); }); 

This will run all queries in parallel, but collect the results in order, which should lead to the fastest overall runtime.

If you want to run them once, you can use async / wait as follows:

 const rp = require('request-promise'); app.get('/', async function(req, res) { let results = []; for (let url of endpoints) { try { let r = await rp(url); if (r) { results.push(url.url); } } catch(e) { // ignore error } } res.json(results); }); 
+1
source

You must wait for all requests to resolve before sending the final_res array back to the client. You can do this with the concepts of async/await and Promise.all . If you do not want to use these resources, you will need to calculate and wait for the entire request manually, using a counter to find out when all the requests were completed, as shown below:

 app.get('/', function(req, res) { var url; var final_res = []; var respCounter = endpoints.length; endpoints.forEach(function(url){ request(url, function(error,response,body){ respCounter--; if(!error && response.statusCode == 200){ final_res.push(url.url); console.log(url.url); }else{ res.send(err); console.log(err); } if(respCounter == 0) { res.send(final_res); } }); }); }); 
0
source

Source: https://habr.com/ru/post/1275685/


All Articles