-1
const express = require('express');
const app     = express();
const path    = require('path');
const config  = require('./config.json');
const moment = require('moment');

const request = require('request-promise');

url = 'https://ne.api.site',
url2 = 'https://n2.api.site',


app.set('view engine', 'pug');
app.use(express.static(path.join(__dirname, 'public')));

async function getApi(){
  return request({
        url : url,
        url2 : url2
    });

}


/* Routes */
app.get('/', async (req, res) => {
  let host = req.get('host');
  let api = await getApi();
  let json = await JSON.parse(api);
  res.render('home', { api: json, moment: require('moment')});
});


app.listen(4800, () => console.log('Express listening on port 4800'));

I am trying to combine the 2 json api urls, and cant seem to figure out what to do. Do I need to create an array? If so how? So I am trying to combine the data from different urls(because of different regions) all onto one main page that can be accessed.

  • only 1 url per request so need to create a separate request for the second url. – Don F. Nov 01 '20 at 20:16
  • Does this answer your question? [Nodejs request-promise combining json api data from 2 links?](https://stackoverflow.com/questions/64649813/nodejs-request-promise-combining-json-api-data-from-2-links) – eol Nov 02 '20 at 17:04

1 Answers1

0

Each API will need its own separate GetAPI operation. There's really no such thing as combining the retrieval of data from multiple API endpoints into one.

You're already using an async function, so you could try something like this pseudocode.

async function isOnline(name){
  const result = {};
  let api   = await getFirstApi();
  let json  = JSON.parse(api);
  result.firstApi = json;
  api   = await getSecondApi();
  json  = JSON.parse(api);
  result.secondApi = json;
  /* whatever comes next */
}
O. Jones
  • 103,626
  • 17
  • 118
  • 172