node中的fetch报错。在浏览器环境使用相同代码可以获取返回内容。但是node环境就会报错(fetch,node-fetch,axios都试过了都是一个错误),但是前几天我却可以正常fetch,没有做任何改动。
下面这个是报错的代码
async function initialize() {
const url = "https://api.hearthstonejson.com/v1/latest/enUS/cards.json";
try {
// 获取卡牌数据
const response = await fetch(url);
cardsdata = await response.json();
console.log(`Cards data loaded: ${cardsdata.length} cards`);
} catch (error) {
console.error('Failed to fetch cards data:', error);
process.exit(1);
}
}
下面这个是报错的信息
PS D:\project\nodejs-project\node1\heartstone> node main.js
Failed to fetch cards data: TypeError: fetch failed
at Object.fetch (node:internal/deps/undici/undici:11457:11)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async initialize (D:\project\nodejs-project\node1\heartstone\main.js:134:20)
cause: Error: read ECONNRESET
at TLSWrap.onStreamRead (node:internal/stream_base_commons:217:20) {
cause: Error: read ECONNRESET
at TLSWrap.onStreamRead (node:internal/stream_base_commons:217:20) {
errno: -4077,
code: 'ECONNRESET',
syscall: 'read'
}
}
不知道什么原因...