reactjsexpressserveraxiosnodejs-express-server

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


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....


Solution

  • 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);
      }
    });