首页 > 编程语言 >How to fix Fetch TypeError in Node.js All In One

How to fix Fetch TypeError in Node.js All In One

时间:2023-09-15 13:23:20浏览次数:39  
标签:Node node TypeError fix Fetch internal https undici fetch

How to fix Fetch TypeError in Node.js All In One

TypeError: terminated at Fetch.onAborted (node:internal/deps/undici/undici:11000:53)

Error

undici

// ❌❌ video url https://edu.xgqfrms.xyz/dc9bb2-1733219a4a8.mp4
TypeError: terminated
    at Fetch.onAborted (node:internal/deps/undici/undici:11000:53)
    at Fetch.emit (node:events:513:28)
    at Fetch.terminate (node:internal/deps/undici/undici:10272:14)
    at Object.onError (node:internal/deps/undici/undici:11095:36)
    at Request.onError (node:internal/deps/undici/undici:6477:31)

image

solution

use node-fetch instead of Node.js undici fetch ❓

$ npm i node-fetch
$ npm i -D @types/node-fetch

https://www.npmjs.com/package/node-fetch

import fetch from 'node-fetch';

// fetch-polyfill.js
import fetch, {
  Blob,
  blobFrom,
  blobFromSync,
  File,
  fileFrom,
  fileFromSync,
  FormData,
  Headers,
  Request,
  Response,
} from 'node-fetch'

if (!globalThis.fetch) {
  globalThis.fetch = fetch
  globalThis.Headers = Headers
  globalThis.Request = Request
  globalThis.Response = Response
}

// index.js
import './fetch-polyfill'

// ...

arrayBuffer & res.clone()

import fetch from 'node-fetch';
const response = await fetch('https://example.com', {
  // About 1MB
  highWaterMark: 1024 * 1024
});

const result = await res.clone().arrayBuffer();
console.dir(result);

demos

  async fetchVideo(url, filepath) {
    const filename = `${filepath}.mp4`;
    return fetch(url)
      // .then((res) => res.arrayBuffer())
      .then((res) => {
        console.log(`loading ... ⏳`);
        return res.arrayBuffer();
      })
      .then((buffer) => {
        console.log(`finished ✅`)
        let arrayBuffer = Buffer.from(buffer)
        fs.writeFile(filename, arrayBuffer, (err) => {
          if (err) {
            console.error(err);
            console.error(`❌ video url`, url);
          } else {
            console.log("video downloaded successfully");
          }
        });
      })
      .catch((err) => console.error(`❌❌ video url`, url, err));
      /*
        ❌❌ video url https://edu.xgqfrms.xyz/dc9bb2-1733219a4a8.mp4
        TypeError: terminated
          at Fetch.onAborted (node:internal/deps/undici/undici:11000:53)
          at Fetch.emit (node:events:513:28)
          at Fetch.terminate (node:internal/deps/undici/undici:10272:14)
          at Object.onError (node:internal/deps/undici/undici:11095:36)
          at Request.onError (node:internal/deps/undici/undici:6477:31)
          at errorRequest (node:internal/deps/undici/undici:8440:17)
          at TLSSocket.onSocketClose (node:internal/deps/undici/undici:7895:9)
          at TLSSocket.emit (node:events:525:35)
          at node:net:313:12
          at TCP.done (node:_tls_wrap:587:7) {
        [cause]: BodyTimeoutError: Body Timeout Error
            at Timeout.onParserTimeout [as _onTimeout] (node:internal/deps/undici/undici:7839:32)
            at listOnTimeout (node:internal/timers:566:11)
            at process.processTimers (node:internal/timers:507:7) {
          code: 'UND_ERR_BODY_TIMEOUT'
        }
      }
      */
  }

(

标签:Node,node,TypeError,fix,Fetch,internal,https,undici,fetch
From: https://www.cnblogs.com/xgqfrms/p/17704563.html

相关文章

  • Nodejs+Express+MongoDB实战
    项目安装安装express脚手架:npminstallexpress-generator-g创建项目:express-eproject-e代表使用ejs模板,project是项目名称进入项目:npminstall下载依赖包安装nodemon:npminstallnodemon-g使用nodemon来启动项目,不用node来启动启动项目:npmstart,端口号在www启动文件中可以......
  • 详解Node.js开发中不可或缺的7个库
    详解Node.js开发中不可或缺的7个库news/2023/9/1414:26:05在Node.js开发中,选择合适的库对于提高开发效率和优化应用程序性能至关重要。本文将介绍七个备受关注的Node.js库,它们在各自的领域中展现了出色的功能和性能。这些库分别是:Config、Fetch、Ioredis、Multer、Cache......
  • nodejs+koa2+mongodb 从0到1搭建自己的项目
    nodejs+koa2+mongodb从0到1搭建自己的项目YDJFE2018-09-2811:2419207   相信这对于想要入门写nodejs的朋友来说,一定会有所收获,那么,下面开始我们的正题(这里不使用koa-generator脚手架,我们直接自己搭建项目,适用于前后端分离)一:创建项目可以在全局中安装koa依赖包......
  • 在nodejs中使用Typescript 金字教程
    1.首先新建一个项目npminit-yes2.开启Typescript依赖npminstalltypescript--save-dev安装typescript,现在我们可以通过命令行来使用tsc命令3.安装nodejs类型npminstall@types/node--save-dev4.使用命令创建一个tsconfig.json文件npxtsc--init--rootDir......
  • js 四舍五入toFixe和Math.round(
    toFixed四舍五入偶尔不正确的问题原因:因为toFixed是一个四舍六入五成双的方法四舍六入好理解,那什么叫五成双呢"五"指的是根据5后面的数字来定,当5后有数时,舍5入1;当5后无有效数字时,需要分两种情况来讲:①5前为奇数,舍5入1;②5前为偶数,舍5不进。(0是偶数)(0.5251).toFixed(2)=>0.53......
  • CentOS6.4邮件配置一: postfix + dovecot + mysql + postfixadmin + Roundcubemail
    参考文章:----------------------------[size=large][color=red]可以考虑:[/color][/size]Posty——Postfix和Dovecot的管理工具[url][/url]Posty是一款基于Web的Postfix&Dovecot管理工具,功能模块包括:API、CLI和WebUI。基于Centos6.2X64系......
  • nodeJs读取JOSN文件导出多个word文件
    前言最近遇到一个需求,读取JOSN文件,以JSON文件的标题为word的文件名导出多个文档,利用nodeJs和 officegen实现了该功能exportWords.js代码如下/***读取指定文件夹下的JSON文件,导出多个word文件*一个json文件导出多个word文件,通常以json文件中的title作为导出的word的文......
  • nodeJS读取JSON文件导出word文档
    前言最近遇到一个需求,将JSON文件的内容,导出到word文档,利用nodeJs和Officegen实现了文件导出的功能exportAWord.js代码如下/***读取指定文件夹下的JSON文件,导出为word*一个json文件为一个word,以json文件的名称为word文件的名称*/letfs=require("fs");letpath=......
  • MySQL Node.js mysql 事务
    MySQLNode.jsmysql事务在MySQL数据库中,事务(transaction)是一组原子性操作,要么全部执行成功,要么全部回滚回去。在多用户并发环境中,事务可以保护数据的一致性和完整性。MySQL提供了ACID属性,并且支持事务。Node.js中,可以通过使用mysql模块的连接来实现事务。什么是事务在MySQL数......
  • Unity 修改 FixedUpdate 时间间隔
    1.依次点击Edit->ProjectSetting->Time后,在打开的窗口中修改FixedTimestep即可。如下图所示: ......