const express = require("express")
const app = express()
const {json, urlencoded} = require("body-parser")
const axios = require('axios')
app.use(urlencoded({ extended: false }))
//app.use(json())
app.get("/" , (req , res) => {
axios.get('https://api.github.com/users/haitruonginfotech')
.then(response => res.send({success : true , response}))
.catch(error => res.send({success : false , message : error}))
})
app.listen("3001",() => console.log("Server started"))
答案 0 :(得分:1)
对代码进行较小的更改即可使其正常工作,该错误不在Axios中,而是在Express中,试图将整个(循环)响应结构转换为Json。
此外,我不会发回整个错误对象,因为这样做不能很好地序列化,最好发送选定的属性,例如消息。
const express = require("express")
const app = express()
const axios = require('axios')
app.get("/" , (req , res) => {
axios.get("https://api.github.com/users/janbodnar")
.then(response => res.send({success : true , response: response.data}))
.catch(error => res.send({success : false , message: error.message}))
})
app.listen("3001",() => console.log("Server started"))