I think it's already too late to answer this question, but recently I ran into the same problem, my use case was to call the JSON API with page numbering, get all the data from each page numbering and add them into one array.
const https = require('https'); const apiUrl = "https://example.com/api/movies/search/?Title="; let finaldata = []; let someCallBack = function(data){ finaldata.push(...data); console.log(finaldata); }; const getData = function (substr, pageNo=1, someCallBack) { let actualUrl = apiUrl + '${substr}&page=${pageNo}'; let mydata = [] https.get(actualUrl, (resp) => { let data = ''; resp.on('data', (chunk) => { data += chunk; }); resp.on('end', async () => { if (JSON.parse(data).total_pages!==null){ pageNo+=1; somCallBack(JSON.parse(data).data); await getData(substr, pageNo, someCallBack); } }); }).on("error", (err) => { console.log("Error: " + err.message); }); } getData("spiderman", pageNo=1, someCallBack);
As @ackuser mentioned, we can use another module, but in my case I had to use the https node. Hope this helps others.
Vaibhav Rai Jun 14 '19 at 9:33 2019-06-14 09:33
source share