Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用节点获取从API获取数据和响应状态?_Javascript_Node.js_Fetch_Node Fetch - Fatal编程技术网

Javascript 如何使用节点获取从API获取数据和响应状态?

Javascript 如何使用节点获取从API获取数据和响应状态?,javascript,node.js,fetch,node-fetch,Javascript,Node.js,Fetch,Node Fetch,根据节点获取文档 我们可以得到这样的响应状态 fetch('https://github.com/') .then(res => { console.log(res.status); }); fetch('https://api.github.com/users/github') .then(res => res.json()) .then(jsonData => {

根据节点获取文档

我们可以得到这样的响应状态

fetch('https://github.com/')
    .then(res => {
        console.log(res.status);
    });
     fetch('https://api.github.com/users/github')
            .then(res => res.json())
            .then(jsonData => {
             console.log(jsonData);
             console.log(jsonData.status);
      });
以及获取数据

fetch('https://api.github.com/users/github')
    .then(res => res.json())
    .then(jsonData => console.log(jsonData));
我有一个场景,需要从响应返回JSON数据和状态。我试着这样用

fetch('https://github.com/')
    .then(res => {
        console.log(res.status);
    });
     fetch('https://api.github.com/users/github')
            .then(res => res.json())
            .then(jsonData => {
             console.log(jsonData);
             console.log(jsonData.status);
      });
但是

console.log(jsonData.status)


不会返回状态。如何获取状态和输出数据

最简单的解决方案是声明一个变量并为其分配
res.status
值:

let status; 
fetch('https://api.github.com/users/github')
  .then((res) => { 
    status = res.status; 
    return res.json() 
  })
  .then((jsonData) => {
    console.log(jsonData);
    console.log(status);
  })
  .catch((err) => {
    // handle error
    console.error(err);
  });
您也可以使用
async/await
以这种方式进行尝试:

retrieveStatus = async (url) => {
  try {
    const response = await fetch(url);
    const { status } = response; 
    return status;
  } catch (err) {
   // handle error
    console.error(err);
  }
}
然后,您可以将其与您想要的任何URL一起使用:


retrieveStatus('https://api.github.com/users/github)

另一个替代解决方案是使用
Promise.all

fetch('https://api.github.com/users/github')
  .then(res => Promise.all([res.status, res.json()]))
  .then(([status, jsonData]) => {
    console.log(jsonData);
    console.log(status);
  });

希望它有帮助

我可能会朝相反的方向走,但是我建议您使用,因为它的社区比节点获取大得多,并且提供了很多功能

使用requestJs,您可以获取如下状态代码

 request("https://api.github.com/users/github", (err, res, body) => {
     let statusCode = res.statusCode;
 });

RequestJs还提供了许多调用不同API方法的简便方法,也可以方便地查看API请求和API响应。

这里还有另一个变体,使用异步lambda函数和数组分解:

fetch('https://api.github.com/users/github')
  .then(async (res) => {  
    return [await res.json(), res.status];
  })
  .then(([jsonData, status]) => {
    console.log(jsonData);
    console.log(status);
  })
  .catch((err) => {
    // handle error
    console.error(err);
  });

看看这个。它给出了如何执行此操作的提示。已弃用可能重复的请求