'I need to create a rest API in node that calls another API

I need to create a REST API in my node app, that GET data from an external API - https://newsapi.org/v2/top-headlines?category=%7Bcategoryname%7D&apiKey=APIKEY

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

Can you help me build a function that would do so?

I am using mongoDB

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:

app.js


router.get('/api/Users/news/:id', controller.newsget);


router.get('/api/Users/news/:id', (req, res) => {
    const id = req.params.id;  
  
    for (let i = 0; i <User.length; i++) {
        let user = User[i]
        if (user.id === id) {
            axios
                .get('http://newsapi.org/v2/top-headlines?country=in&category=general&apiKey=36f3e29b704f41339af8439dc1228334')
                .then(response => {
                let userData = response.data;
                res.send(userData);})
                  
                
        }
    }
  
  });

controller.js

exports.newsget = (req, res)=>{
    if(!req.body){
        return res
            .status(400)
            .send({ message : "Data to update can not be empty"})
    }

    const id = req.params.id;
    User.findByIdAndUpdate(id, req.body, { useFindAndModify: false})
        .then(data => {
            if(!data){
                res.status(404).send({ message : `Cannot Update user with ${id}. Maybe user not found!`})
            }else{
                res.send(data)
            }
        })
        .catch(err =>{
            res.status(500).send({ message : "Error Update user information"})
        })
}

I have very little clue on the approach, but i badly need assistance. Please help

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



Solution 1:[1]

First of all, you are writing two GET methods for same route, from what I am being concluding that, the first route should be POST according to what I am suspecting to be your functionality is depicted as follows


router.post('/api/Users/news/:id', controller.newsget);


router.get('/api/Users/news/:id', (req, res) => {
    const id = req.params.id;  
  
    for (let i = 0; i <User.length; i++) {
        let user = User[i]
        if (user.id === id) {
            axios
                .get('http://newsapi.org/v2/top-headlines?country=in&category=general&apiKey=36f3e29b704f41339af8439dc1228334')
                .then(response => {
                let userData = response.data;
                res.send(userData);})
                  
                
        }
    }
  
  });

Also if you are picking the logged in user, use req.user.id

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gautam Dhiman