node-fetch Advanced Usage All In One
node-fetch Advanced Usage All In One
fetch
// stream
https://www.npmjs.com/package/node-fetch#streams
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
*/
refs
https://byby.dev/node-download-image
©xgqfrms 2012-2021
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
未经授权禁止转载,违者必究!