Node.js Can';I don’我不会提出两次请求

Node.js Can';I don’我不会提出两次请求,node.js,express,Node.js,Express,因此,我设置了这个路由,它从内部API获取JSON数据并显示出来。第一次访问路由时,我得到了正确的JSON数据,但当我刷新页面时,我得到了一个错误,页面只是在加载。有什么想法吗? 注:我使用请求承诺 app.get('/',function(req,res){ var options = { //Here I initiate the API call.I actually have a url here. uri: '', json: true }; request(options)

因此,我设置了这个路由,它从内部API获取JSON数据并显示出来。第一次访问路由时,我得到了正确的JSON数据,但当我刷新页面时,我得到了一个错误,页面只是在加载。有什么想法吗?
注:我使用请求承诺

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

控制台中出现错误,页面无法加载,因为您调用的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
        });
  });

“当我刷新页面时,我得到一个错误”-这是…?HPE_无效_常量。我删除了缓存,现在它工作了,但我不知道缓存是如何相关的。我缓存来自API的响应。“当我刷新页面时,我得到一个错误”-这是…?HPE_无效_常量。我删除了缓存,现在它工作了,但我不知道缓存是如何相关的。我缓存来自API的响应。只是一个简单的例子,我可能在这里出错,但在
然后
块中`我们不应该按照相应的res.end执行res.send(data)吗?我知道oyu并没有在不启动res.send()方法的情况下思考这个问题。响应永远不会成功:pres.send()为您调用res.end()。@Jackthomson确实阅读了上的文档。它在JSON内容中扮演
res.send()
的角色。agree@E_net4调用res.JSON后无需调用res.end只是一个简单的例子我可能在这里错了,但是在
然后
块中`我们不应该按照res.end进行res.send(数据)吗?我知道oyu并没有在不启动res.send()方法的情况下思考这个问题。响应永远不会成功:pres.send()为您调用res.end()。@Jackthomson确实阅读了上的文档。JSON content.agree@E_net4在调用res.JSON后无需调用res.end