如何使用 EXPRESS 和 AXIOS 从第三方 api 获取数据?
how to fetch data from third party api using EXPRESS and AXIOS?
伙计们.....
所以我想从第三方获取数据 api 但问题是数据已获取但未显示在控制台中.....意味着我 运行 我的服务器数据显示在终端上,但它没有显示在控制台中,而是 localhost 继续加载,没有任何显示...
这是代码...
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 连续加载....
在 express 服务器中快速加载和挂起是因为您应该在 express get 调用中调用 next()
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()
})
您需要使用 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);
}
});
伙计们.....
所以我想从第三方获取数据 api 但问题是数据已获取但未显示在控制台中.....意味着我 运行 我的服务器数据显示在终端上,但它没有显示在控制台中,而是 localhost 继续加载,没有任何显示...
这是代码...
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 连续加载....
在 express 服务器中快速加载和挂起是因为您应该在 express get 调用中调用 next()
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()
})
您需要使用 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);
}
});