'External API called in a route, and testing the route in node JS

I have the below route created in my application which calls another API for data:

newsr.get('/news', async (req, res) => {
    var category = req.params.category;
    try {
        var url = 'http://newsapi.org/v2/top-headlines?country=in&category=general' + '&apiKey=36f3e29b704f41339af8439dc1228334';

        const news_get = await axios.get(url)
        res.status(200).json().render('category', { articles: news_get.data.articles })
        // return res.status(200).json(news_get)
    } catch (error) {
        if (error.response) {
            console.log(error)
        }
    }
})

The problem is, when I call the get API http://localhost:8080/news. I do not get the response from the external API. Can you please suggest what to do?



Sources

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

Source: Stack Overflow

Solution Source