xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

node-fetch Advanced Usage All In One

node-fetch Advanced Usage All In One

fetch

// stream

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

image

demos

Node.js web crawler


import fetch from "node-fetch";

import path from 'node:path';
import {fileURLToPath} from 'node:url';

// import fs from 'node:fs';
import {createWriteStream} from 'node:fs';
import {pipeline} from 'node:stream';
import {promisify} from 'node:util'

// const __filename = fileURLToPath(import.meta.url);
// const __dirname = path.dirname(__filename);
// console.log(`import.meta.url`, import.meta.url)
// console.log(`__dirname`, __dirname)


async function downloadFile(url, path) {
  const streamPipeline = promisify(pipeline);
  fetch(url).then(async (res) => {
    if (!res.ok) {
      throw new Error(`unexpected response ${res.statusText}`);
    }
    console.log(`✅ res =`, res)
    return await streamPipeline(res.body, createWriteStream(path));
  }).catch(err => {
    console.log(`❌ err =`, err)
  }).finally(() => {
    console.log(`finally 👻`)
  })
}

// async function downloadFile(url, path) {
//   const streamPipeline = promisify(pipeline);
//   const res = await fetch(url)
//   if (!res.ok) {
//     throw new Error(`unexpected response ${res.statusText}`);
//   }
//   // console.log(`✅ res =`, res)
//   await streamPipeline(res.body, createWriteStream(path));
// }


// const url = `https://edu-vod.lagou.com/sv/2daa3bb9-1765150ee6e/2daa3bb9-1765150ee6e.mp4`
// const url = `https://cdn.xgqfrms.xyz/video/web-testing.mp4`
const url = `https://cdn.xgqfrms.xyz/video/web-testing.mp5`
await downloadFile(url, "./test.mp4");

/*

$ node ./node-fetch.js

*/


axios

import axios from 'axios';
// import axios, {isCancel, AxiosError} from 'axios';

// import fs from 'node:fs';
import {createWriteStream} from 'node:fs';

// import path from 'path';
// import { fileURLToPath } from 'url';

// const __filename = fileURLToPath(import.meta.url);
// const __dirname = path.dirname(__filename);
// console.log(`import.meta.url`, import.meta.url)
// console.log(`__dirname`, __dirname)

// async function downloadFile(url, path) {
//   const res = await axios({
//     url,
//     method: "GET",
//     responseType: "stream",
//   });
//   console.log(`✅ content-type =`, res.headers['content-type'])
//   res.data.pipe(createWriteStream(path));
// }

async function downloadFile(url, path) {
  await axios({
    url,
    method: "GET",
    responseType: "stream",
  }).then(res => {
    console.log(`✅ content-type =`, res.headers['content-type'])
    return res.data.pipe(createWriteStream(path));
  }).catch(err => {
    console.log(`❌ err =`, err)
  }).finally(() => {
    console.log(`finally 👻`)
  })
}


// const url = `https://edu-vod.lagou.com/sv/2daa3bb9-1765150ee6e/2daa3bb9-1765150ee6e.mp4`
const url = `https://cdn.xgqfrms.xyz/video/web-testing.mp4`
await downloadFile(url, "./test.mp4");


/*

$ node ./axios.js

*/

Streams

Types of streams

There are four fundamental stream types within Node.js:

  1. Writable: streams to which data can be written (for example, fs.createWriteStream()).
  2. Readable: streams from which data can be read (for example, fs.createReadStream()).
  3. Duplex: streams that are both Readable and Writable (for example, net.Socket).
  4. Transform: Duplex streams that can modify or transform the data as it is written and read (for example, zlib.createDeflate()).

Additionally, this module includes the utility functions stream.pipeline(), stream.finished(), stream.Readable.from() and stream.addAbortSignal().

import { pipeline } from 'node:stream/promises';
import { createReadStream, createWriteStream } from 'node:fs';
import { createGzip } from 'node:zlib';

await pipeline(
  createReadStream('archive.tar'),
  createGzip(),
  createWriteStream('archive.tar.gz'),
);

console.log('Pipeline succeeded.');

https://nodejs.org/dist/latest-v18.x/docs/api/stream.html

The WHATWG Streams Standard (or "web streams") defines an API for handling streaming data.
It is similar to the Node.js Streams API but emerged later and has become the "standard" API for streaming data across many JavaScript environments.

There are three primary types of objects:

  1. ReadableStream - Represents a source of streaming data.
  2. WritableStream - Represents a destination for streaming data.
  3. TransformStream - Represents an algorithm for transforming streaming data.
import {
  ReadableStream,
} from 'node:stream/web';

import {
  setInterval as every,
} from 'node:timers/promises';

import {
  performance,
} from 'node:perf_hooks';

const SECOND = 1000;

const stream = new ReadableStream({
  async start(controller) {
    for await (const _ of every(SECOND))
      controller.enqueue(performance.now());
  },
});

for await (const value of stream)
  console.log(value);

https://nodejs.org/dist/latest-v18.x/docs/api/webstreams.html

Modules: node:module API

ESM

// module.mjs
// In an ECMAScript module
import { builtinModules as builtin } from 'node:module';

CJS

// module.cjs
// In a CommonJS module
const builtin = require('node:module').builtinModules;

https://nodejs.org/dist/latest-v18.x/docs/api/module.html

node:fs vs fs

import fs from 'node:fs';

import fs from 'fs';

node: 类似 https://, file:// 是一种通信协议 ❓

https://stackoverflow.com/questions/67554506/what-are-the-nodefs-nodepath-etc-modules

refs

https://byby.dev/node-download-image



©xgqfrms 2012-2025

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @   xgqfrms  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
历史上的今天:
2022-08-27 iPhone 外接移动固态硬盘 All In One
2022-08-27 三星固态硬盘 All In One
2022-08-27 2022 最新中国电影票房排行榜 All In One
2021-08-27 how to using js get canvas text content All In One
2020-08-27 Dart Generic All In One
2020-08-27 how to create react custom hooks with arguments All In One
2020-08-27 Go lang All In One
点击右上角即可分享
微信分享提示