簡體   English   中英

如何使用 EXPRESS 和 AXIOS 從第三方 api 獲取數據?

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

伙計們.....

所以我想從第三方 api 獲取數據,但問題是數據被獲取但它沒有顯示在控制台中.....意味着當我運行我的服務器時,數據會顯示在終端上但它沒有顯示在控制台中,而不是本地主機繼續加載並且沒有顯示任何內容......

這是代碼...

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

對此的任何解決方案如何在控制台中顯示數據並停止 * localhost連續加載....

您需要使用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); } });

在 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