Fetch 报错

常规

排查 SocketError other side closed

在 main.js 中使用了 fetch,但是在运行 main.js 的时候时候 fetch 报错。

% node main.js
 
(node:51258) Warning: Setting the NODE_TLS_REJECT_UNAUTHORIZED environment variable to '0' makes TLS connections and HTTPS requests insecure by disabling certificate verification.
(Use `node --trace-warnings ...` to show where the warning was created)
node:internal/deps/undici/undici:11730
    Error.captureStackTrace(err, this);
          ^
 
TypeError: fetch failed
    at Object.fetch (node:internal/deps/undici/undici:11730:11)
    at async getFansNum (/Users/kazoottt/GitHub/bilibili-fans/main.js:11:20) {
  cause: SocketError: other side closed
      at TLSSocket.onSocketEnd (node:internal/deps/undici/undici:8280:26)
      at TLSSocket.emit (node:events:526:35)
      at endReadableNT (node:internal/streams/readable:1589:12)
      at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
    code: 'UND_ERR_SOCKET',
    socket: {
      localAddress: '198.19.0.1',
      localPort: 55306,
      remoteAddress: '198.18.2.185',
      remotePort: 443,
      remoteFamily: 'IPv4',
      timeout: undefined,
      bytesWritten: 607,
      bytesRead: 0
    }
  }
}
 
Node.js v20.10.0

从报错信息中可以看出来是 getFansNum 这个方法中的 fetch 方法报了错。

解决方法:

关闭 MitM