title: I need to create a rest API in node that calls another API
body: <p>I need to create a REST API in my node app, that GET data from an external API - <a href="https://newsapi.org/v2/top-headlines?category=%7Bcategoryname%7D&amp;apiKey=APIKEY" rel="nofollow noreferrer">https://newsapi.org/v2/top-headlines?category=%7Bcategoryname%7D&amp;apiKey=APIKEY</a></p>
  <p>The condition is that this rest API should contain id of the user in DB.
  Only when we trigger an API with valid userID, it should return response as the data coming from external API.
  otherwise show error</p>
  <p>Can you help me build a function that would do so?</p>
  <p>I am using mongoDB</p>
  <p>I am writing few snippets of code i wrote to accomplish this, but i am pretty bad at it. So any help on this will be highly appreciated:</p>
  <p>app.js</p>
  <pre><code>
  router.get('/api/Users/news/:id', controller.newsget);
  
  
  router.get('/api/Users/news/:id', (req, res) =&gt; {
      const id = req.params.id;  
    
      for (let i = 0; i &lt;User.length; i++) {
          let user = User[i]
          if (user.id === id) {
              axios
                  .get('http://newsapi.org/v2/top-headlines?country=in&amp;category=general&amp;apiKey=36f3e29b704f41339af8439dc1228334')
                  .then(response =&gt; {
                  let userData = response.data;
                  res.send(userData);})
                    
                  
          }
      }
    
    });
  </code></pre>
  <p>controller.js</p>
  <pre><code>exports.newsget = (req, res)=&gt;{
      if(!req.body){
          return res
              .status(400)
              .send({ message : &quot;Data to update can not be empty&quot;})
      }
  
      const id = req.params.id;
      User.findByIdAndUpdate(id, req.body, { useFindAndModify: false})
          .then(data =&gt; {
              if(!data){
                  res.status(404).send({ message : `Cannot Update user with ${id}. Maybe user not found!`})
              }else{
                  res.send(data)
              }
          })
          .catch(err =&gt;{
              res.status(500).send({ message : &quot;Error Update user information&quot;})
          })
  }
  </code></pre>
  <p>I have very little clue on the approach, but i badly need assistance. Please help</p>
  <p>I have tried mimicking some online functions to search the user and then try to fetch data from external API if the user's ID was present in my DB. But it failed</p>