Javascript 如何从express server返回json?
我用express.js构建了一个服务器,其中一部分如下所示:Javascript 如何从express server返回json?,javascript,json,reactjs,axios,Javascript,Json,Reactjs,Axios,我用express.js构建了一个服务器,其中一部分如下所示: app.get("/api/stuff", (req, res) => { axios.get('http://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b1b15e88fa797225412429c1c50c122a1').then(function(response){ res.send(response); co
app.get("/api/stuff", (req, res) => {
axios.get('http://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b1b15e88fa797225412429c1c50c122a1').then(function(response){
res.send(response);
console.log('response=',response);
})
});
当我点击“api/stuff”时,它返回一个错误:
(节点:1626)未处理的PromisejectionWarning:未处理的承诺
拒绝(拒绝id:1):类型错误:转换圆形结构
到JSON
如何从端点返回json?从open weather API获得的
响应
对象是循环类型(引用自身的对象)<当通过循环引用时,code>JSON.stringify将抛出错误。这就是使用send
方法时出现此错误的原因
为了避免这种情况,只需将所需数据作为响应发送
app.get("/api/stuff", (req, res) => {
axios.get('http://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b1b15e88fa797225412429c1c50c122a1').then(function(response){
res.send(response.data);
console.log('response=',response.data);
})
});