admin管理员组

文章数量:1278653

Hi all I have to develop a utility which makes a call to external API with different parameters, for example, I have an array val which has 100 value val= ['we23','22ww', 'gh22'....n] and URL: www.google so one by one I have to append value from val to the URL, first api= www.google/we23, second api= www.google/22ww and make an External API hit and then store the response in database. so what is the most efficient way to do it? and links to working examples would be helpful.

Hi all I have to develop a utility which makes a call to external API with different parameters, for example, I have an array val which has 100 value val= ['we23','22ww', 'gh22'....n] and URL: www.google. so one by one I have to append value from val to the URL, first api= www.google./we23, second api= www.google./22ww and make an External API hit and then store the response in database. so what is the most efficient way to do it? and links to working examples would be helpful.

Share Improve this question asked Feb 22, 2020 at 15:14 K.M.JK.M.J 1431 gold badge4 silver badges13 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

A very simple example express app using the Fetch API:

const express = require('express')
const fetch = require('node-fetch')
const app = express()

// This sets up a route to localhost:3000/random and goes off and hits
// cat-fact.herokuapp./facts/random
app.get('/:apiRoute', async (req, res) => {
  try {
    const { apiRoute } = req.params
    const apiResponse = await fetch(
      'https://cat-fact.herokuapp./facts/' + apiRoute
    )
    const apiResponseJson = await apiResponse.json()
    // await db.collection('collection').insertOne(apiResponseJson)
    console.log(apiResponseJson)
    res.send('Done – check console log')
  } catch (err) {
    console.log(err)
    res.status(500).send('Something went wrong')
  }
})

app.listen(3000, () => console.log(`Example app listening on port 3000!`))

Visit http://localhost:3000/random

With the following code you can make concurrent API calls within an endpoint using Node.js + Express:

const [
      LoMasNuevo, LoMasVisto, TeReendamos, Categorias,
    ] = await Promise.all([
      numerosController.getLoMasNuevo(),
      numerosController.getLoMasVisto(),
      numerosController.getReendaciones(),
      categoriasController.getCategorias(),
    ]);

Inside every get function you can make an axios request like this:

const params = {
    method: 'GET',
    url: 'https://development.api.yodlee./ysl/transactions',
    headers: {
        'Api-Version': '1.1',
        Authorization: `Bearer ${tokenuser}`,
    },
};

const data = await axios(params);
return data;

In 2022

In Node.js:

const fetch = (...args) => import('node-fetch').then(({ default: fetch }) =>
    fetch(...args));

app.get('/checkDobleAPI', async (req, res) => {
    try {

        const apiResponse = await fetch(
            'https://jsonplaceholder.typicode./posts'
        )
        const apiResponseJson = await apiResponse.json()

        console.log(apiResponseJson)
        res.send('Running 

本文标签: javascriptHow to make call to external api from nodejsStack Overflow