how to fetch data from third party api using EXPRESS and AXIOS?

2.7k Views Asked by At

guys.....

so I want to fetch data from a third party api but the problem is the data is fetched but its not displaying in the console..... means when ever I run my server the data gets displayed on the terminal but its not getting displayed in the console rather the localhost keeps on loading and nothing gets displayed...

here's the code...

const express = require('express')
const axios = require('axios')

const app = express()

const axiosInstance = axios.create({
    baseURL: 'https://api.bittrex.com/api/v1.1/public',
    header: { 'Access-Control-Allow_Origin': '*' }
})
app.get('/', async(req, res, next) => {
        const response = await axiosInstance.get('/getmarketsummaries')
        console.log(response.data.result)

})

app.listen(3000, () => {
    console.log('listening on port 3000')
})

any solution to this how can I show the data in console and stop *localhost from continuous loading....

2

There are 2 best solutions below

3
On BEST ANSWER

You need to send the response using send method or you can use json method

app.get("/", async (req, res, next) => {
  try {
    const response = await axiosInstance.get("/getmarketsummaries");
    console.log(response.data.result);

    //You need To send data from using send method
    res.status(200).send(response.data.result);

    //Or you can use json method to send the data
    res.status(200).json(response.data.result);

  } catch (err) {
    res.status(400).send(err);
  }
});

3
On

Rapidly loading and hanging In express server is because you should call next() in your express get call

app.get('/', async(req, res, next) => {
        const response = await axiosInstance.get('/getmarketsummaries')
        console.log(response.data.result)
        
        res.send.status(200).json(response.data.result);
        next()
})