I'm trying to get 2 APIs into one page using Nodejs and Express. I've been researching of the issue and there comes using Async and Promise but couldn't apply the right fit answer for my code. Is there any ways that I can get data from 2 APIs which are 'url' and 'url_test' to a single page which is 'index.ejs'?
server.js
app.get('/users', function (req, res) {
var url = apiUrl + '/users' + apiKey,
url_test = apiUrl + '/projects' + apiKey;
request.get(url, function (error, response, body) {
var bodyData = parseJSON(body);
res.render('index', {
apiData: bodyData
});
});
request.get(url_test, function (error, response, body) {
var bodyData = parseJSON(body);
res.render('index', {
apiData-test: bodyData
});
});
});
index.ejs
<div>
<% for (var i = 0; i < (apiData.users).length; i++) { %>
<div><%= (apiData.users[i]).username %></div>
<% } %>
<hr>
<% for (var i = 0; i < (apiData-test.projects).length; i++) { %>
<div><%= (apiData-test.projects[i]).id%></div>
<% } %>
</div>