简体   繁体   English

如何使用 EXPRESS 和 AXIOS 从第三方 api 获取数据?

[英]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...所以我想从第三方 api 获取数据,但问题是数据被获取但它没有显示在控制台中.....意味着当我运行我的服务器时,数据会显示在终端上但它没有显示在控制台中,而不是本地主机继续加载并且没有显示任何内容......

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....对此的任何解决方案如何在控制台中显示数据并停止 * localhost连续加载....

You need to send the response using send method or you can use json method您需要使用send方法发送响应,或者您可以使用json方法

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

Rapidly loading and hanging In express server is because you should call next() in your express get call在 express 服务器中快速加载和挂起是因为您应该在 express get 调用中调用 next()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM