本地获取 API 时自签名证书错误

Ada*_*nar 0 javascript certificate node.js asp.net-web-api asp.net-core-webapi

我有一个简单的.NET Core WebAPI没有身份验证的。我使用默认策略添加了 Cors。我从 React 网站或 Postman 连接和获取数据没有问题(一切都在我的机器上本地运行)。现在,我尝试在超级简单的node应用程序中从该 API 获取数据,但收到此错误:

file:///Users/aw/Projects/TestNodeApp/node_modules/node-fetch/src/index.js:94
            reject(new FetchError(`request to ${request.url} failed, reason: ${error.message}`, 'system', error));
                   ^

FetchError: request to https://localhost:5001/api/teams failed, reason: self signed certificate
    at ClientRequest.<anonymous> (file:///Users/aw/Projects/TestNodeApp/node_modules/node-fetch/src/index.js:94:11)
    at ClientRequest.emit (node:events:394:28)
    at TLSSocket.socketErrorListener (node:_http_client:447:9)
    at TLSSocket.emit (node:events:394:28)
    at emitErrorNT (node:internal/streams/destroy:157:8)
    at emitErrorCloseNT (node:internal/streams/destroy:122:3)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  type: 'system',
  errno: 'DEPTH_ZERO_SELF_SIGNED_CERT',
  code: 'DEPTH_ZERO_SELF_SIGNED_CERT',
  erroredSysCall: undefined
}
Run Code Online (Sandbox Code Playgroud)

这是我的整个node申请:

import fetch from 'node-fetch';

async function fetchTeams() {
  const response = await fetch('https://localhost:5001/api/teams', { method: 'GET' });
  const data = await response.json();
  return data;
}

(async () => {
  console.log('Process started');
  const teams = await fetchTeams();
  console.log(teams);
})().finally(() => {
  console.log('Process finished');
});
Run Code Online (Sandbox Code Playgroud)

这是什么意思?我缺少什么?

顺便提一句。当我获取 Github API 时,它工作正常,如下所示:

async function fetchGithub() {
  const response = await fetch('https://api.github.com/users/Microsoft');
  const data = await response.json();
  return data;
}
Run Code Online (Sandbox Code Playgroud)

所以我认为 API 中缺少一些东西。我的 React 网站不需要的东西,节点应用程序需要。

感谢帮助!

小智 5

您可以使用此命令来设置NODE_TLS_REJECT_UNAUTHORIZED环境变量:

export NODE_TLS_REJECT_UNAUTHORIZED=0
Run Code Online (Sandbox Code Playgroud)