0

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

  • can you fetch the result without creating instance? – Jatin Mehrotra Jan 23 '22 at 05:53
  • What do you mean by console here? – cEeNiKc Jan 23 '22 at 06:04
  • @JatinMehrotra I haven't tried it. can you tell me how to do that.... –  Jan 23 '22 at 06:08
  • @cEeNiKc I want the data to get shown in the console (in chrome) but its not displaying anything and the ***localhost*** keeps on loading.... but data is displayed in the terminal.... –  Jan 23 '22 at 06:11
  • 1
    As Anil said you need to send the response using .send or .json method otherwise request will hang up till it's timedout. For showing the data in chrome console you need to do console.log on client side from where you make the API call. Doing console.log on server side will only show data in terminal. – cEeNiKc Jan 23 '22 at 06:58
  • yes got it! thank a lot to you also @cEeNiKc :):) –  Jan 23 '22 at 09:16

2 Answers2

2

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);
  }
});
Anil Kumar
  • 465
  • 4
  • 10
  • thanks a lot :):):) it did work just fine... the data is displaying on the browser.... and that's just great... but can you show a way to how can I console log the data because its showing noting in the console.... –  Jan 23 '22 at 06:19
  • 1
    I think consoling API data into the browser console needs front end integration, you need to fetch API on the client end and there you can log the response to the browser, instead of checking response data into the console, you can use browser inspect element, and go to the network tab where you can check your response. – Anil Kumar Jan 23 '22 at 07:07
  • ya exactly... I tried it connecting with react and it worked ! thank you so much –  Jan 23 '22 at 09:15
0

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()
})
  • I tried it ***localhost*** is not continuously loading great ! but an error has been logged in the console ```GET http://localhost:3000/ 404 (Not Found)``` and also the browser is showing ```cannot GET / ```.... –  Jan 23 '22 at 06:07
  • This is not correct. A GET call should send a response and not call `next()`. – jfriend00 Jan 23 '22 at 10:22
  • yes that true thsnk's for that :) – web developer Jan 23 '22 at 10:44