所以,我设置了这个路由,它从内部 API 获取 JSON 数据并显示它。我第一次访问该路由时,我得到了正确的 JSON 数据,但当我刷新页面时,我收到错误,并且页面只是卡在加载中。有什么想法吗?
注意:我使用请求 promise
app.get('/',function(req,res){
var options = {
//Here I initiate the API call.I actually have a url here.
uri: '',
json: true
};
request(options)
.then(function(data) {
res.json(data);
res.end();
})
.catch(function (err) {
console.log(err);
});
});
最佳答案
您在控制台中收到错误,并且页面卡住加载,因为您调用的 api 给出了错误响应,但您没有在 catch 回调中发送任何内容。试试这个。
app.get('/',function(req,res){
var options = {
//Here I initiate the API call.I actually have a url here.
uri: '',
json: true
};
request(options)
.then(function(data) {
res.json(data);
res.end();
})
.catch(function (err) {
console.log(err);
res.status(500).send(err); // set proper error code and send response here
});
});
关于node.js - 不能重复请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42854309/