โมดูลน้ำหนักเบาที่นำ Fetch API ไปยัง Node.js

คุณอาจกำลังมองหาเอกสาร v2
แทนที่จะใช้ XMLHttpRequest ใน Node.js เพื่อเรียกใช้ Polyfill Fetch เฉพาะเบราว์เซอร์ทำไมไม่ไปจาก http ดั้งเดิมไปยัง fetch API โดยตรง? ดังนั้น node-fetch , รหัสน้อยที่สุดสำหรับ window.fetch API ที่ใช้งานร่วมกันได้บนรันไทม์ node.js
ดู isomorphic-unfetch ของ Jason Miller หรือ Cross-Fetch ของ Leonardo Quixada สำหรับการใช้งาน isomorphic (ส่งออก node-fetch สำหรับฝั่งเซิร์ฟเวอร์ whatwg-fetch สำหรับฝั่งไคลเอ็นต์)
window.fetch APIres.text() และ res.json() ) เป็น UTF-8 โดยอัตโนมัติwindow.fetch รับข้อเสนออย่าลังเลที่จะเปิดปัญหา การปล่อยเสถียรในปัจจุบัน ( 3.x ) ต้องใช้อย่างน้อย node.js 12.20.0
npm install node-fetch import fetch from 'node-fetch' ; node-fetch จาก V3 เป็นโมดูล ESM เท่านั้น-คุณไม่สามารถนำเข้าด้วย require()
หากคุณไม่สามารถเปลี่ยนเป็น ESM ได้โปรดใช้ V2 ซึ่งยังคงเข้ากันได้กับ CommonJS การแก้ไขข้อผิดพลาดที่สำคัญจะยังคงเผยแพร่สำหรับ V2
npm install node-fetch@2 หรือคุณสามารถใช้ฟังก์ชั่น assync import() จาก CommonJs เพื่อโหลด node-fetch แบบอะซิงโครนัส:
// mod.cjs
const fetch = ( ... args ) => import ( 'node-fetch' ) . then ( ( { default : fetch } ) => fetch ( ... args ) ) ; ในการใช้ fetch() โดยไม่ต้องนำเข้าคุณสามารถแก้ไขวัตถุ global ในโหนด:
// 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'
// ... ใช้ Node-Fetch เวอร์ชันเก่าหรือไม่ ตรวจสอบไฟล์ต่อไปนี้:
หมายเหตุ: เอกสารด้านล่างนั้นทันสมัยพร้อมรุ่น 3.x หากคุณใช้เวอร์ชันเก่า ๆ โปรดตรวจสอบวิธีการอัพเกรด
import fetch from 'node-fetch' ;
const response = await fetch ( 'https://github.com/' ) ;
const body = await response . text ( ) ;
console . log ( body ) ; import fetch from 'node-fetch' ;
const response = await fetch ( 'https://api.github.com/users/github' ) ;
const data = await response . json ( ) ;
console . log ( data ) ; import fetch from 'node-fetch' ;
const response = await fetch ( 'https://httpbin.org/post' , { method : 'POST' , body : 'a=1' } ) ;
const data = await response . json ( ) ;
console . log ( data ) ; import fetch from 'node-fetch' ;
const body = { a : 1 } ;
const response = await fetch ( 'https://httpbin.org/post' , {
method : 'post' ,
body : JSON . stringify ( body ) ,
headers : { 'Content-Type' : 'application/json' }
} ) ;
const data = await response . json ( ) ;
console . log ( data ) ; URLSearchParams มีอยู่ในวัตถุทั่วโลกใน node.js ณ v10.0.0 ดูเอกสารอย่างเป็นทางการสำหรับวิธีการใช้งานเพิ่มเติม
หมายเหตุ: ส่วนหัว Content-Type จะถูกตั้งค่าโดยอัตโนมัติเป็น x-www-form-urlencoded เมื่ออินสแตนซ์ของ URLSearchParams ได้รับเช่น:
import fetch from 'node-fetch' ;
const params = new URLSearchParams ( ) ;
params . append ( 'a' , 1 ) ;
const response = await fetch ( 'https://httpbin.org/post' , { method : 'POST' , body : params } ) ;
const data = await response . json ( ) ;
console . log ( data ) ; หมายเหตุ: การตอบกลับ 3xx-5xx ไม่ใช่ ข้อยกเว้นและควรจัดการใน then() ดูส่วนถัดไป
การห่อฟังก์ชั่นการดึงข้อมูลลงในบล็อก try/catch จะจับข้อยกเว้น ทั้งหมด เช่นข้อผิดพลาดที่มาจากไลบรารีโหนดคอร์เช่นข้อผิดพลาดของเครือข่ายและข้อผิดพลาดในการดำเนินงานซึ่งเป็นอินสแตนซ์ของ FetchERROR ดูเอกสารการจัดการข้อผิดพลาดสำหรับรายละเอียดเพิ่มเติม
import fetch from 'node-fetch' ;
try {
await fetch ( 'https://domain.invalid/' ) ;
} catch ( error ) {
console . log ( error ) ;
}เป็นเรื่องปกติที่จะสร้างฟังก์ชั่นผู้ช่วยเพื่อตรวจสอบว่าการตอบสนองไม่มีไคลเอนต์ (4xx) หรือเซิร์ฟเวอร์ (5xx) การตอบกลับข้อผิดพลาด:
import fetch from 'node-fetch' ;
class HTTPResponseError extends Error {
constructor ( response ) {
super ( `HTTP Error Response: ${ response . status } ${ response . statusText } ` ) ;
this . response = response ;
}
}
const checkStatus = response => {
if ( response . ok ) {
// response.status >= 200 && response.status < 300
return response ;
} else {
throw new HTTPResponseError ( response ) ;
}
}
const response = await fetch ( 'https://httpbin.org/status/400' ) ;
try {
checkStatus ( response ) ;
} catch ( error ) {
console . error ( error ) ;
const errorBody = await error . response . text ( ) ;
console . error ( `Error body: ${ errorBody } ` ) ;
}คุกกี้จะไม่ถูกเก็บไว้ตามค่าเริ่มต้น อย่างไรก็ตามคุกกี้สามารถสกัดและส่งผ่านโดยการจัดการคำขอและส่วนหัวการตอบสนอง ดูส่วนหัวของ Set-Cookie สำหรับรายละเอียด
"node.js way" คือการใช้สตรีมเมื่อเป็นไปได้ คุณสามารถใช้ท่อ res.body ไปยังสตรีมอื่น ตัวอย่างนี้ใช้ Stream.pipeline เพื่อแนบตัวจัดการข้อผิดพลาดสตรีมและรอให้การดาวน์โหลดเสร็จสมบูรณ์
import { createWriteStream } from 'node:fs' ;
import { pipeline } from 'node:stream' ;
import { promisify } from 'node:util'
import fetch from 'node-fetch' ;
const streamPipeline = promisify ( pipeline ) ;
const response = await fetch ( 'https://github.githubassets.com/images/modules/logos_page/Octocat.png' ) ;
if ( ! response . ok ) throw new Error ( `unexpected response ${ response . statusText } ` ) ;
await streamPipeline ( response . body , createWriteStream ( './octocat.png' ) ) ; ใน node.js 14 คุณยังสามารถใช้ตัววนซ้ำ async เพื่ออ่าน body ; อย่างไรก็ตามระวังการจับข้อผิดพลาด - การตอบสนองที่ยาวขึ้นจะมีโอกาสมากขึ้นที่จะพบข้อผิดพลาด
import fetch from 'node-fetch' ;
const response = await fetch ( 'https://httpbin.org/stream/3' ) ;
try {
for await ( const chunk of response . body ) {
console . dir ( JSON . parse ( chunk . toString ( ) ) ) ;
}
} catch ( err ) {
console . error ( err . stack ) ;
} ใน node.js 12 คุณสามารถใช้ตัววนซ้ำ async เพื่ออ่าน body ; อย่างไรก็ตามตัววนซ้ำ Async ที่มีสตรีมไม่เติบโตจนกระทั่ง Node.js 14 ดังนั้นคุณต้องทำงานพิเศษบางอย่างเพื่อให้แน่ใจว่าคุณจัดการกับข้อผิดพลาดโดยตรงจากสตรีมและรอการตอบสนองอย่างเต็มที่
import fetch from 'node-fetch' ;
const read = async body => {
let error ;
body . on ( 'error' , err => {
error = err ;
} ) ;
for await ( const chunk of body ) {
console . dir ( JSON . parse ( chunk . toString ( ) ) ) ;
}
return new Promise ( ( resolve , reject ) => {
body . on ( 'close' , ( ) => {
error ? reject ( error ) : resolve ( ) ;
} ) ;
} ) ;
} ;
try {
const response = await fetch ( 'https://httpbin.org/stream/3' ) ;
await read ( response . body ) ;
} catch ( err ) {
console . error ( err . stack ) ;
} import fetch from 'node-fetch' ;
const response = await fetch ( 'https://github.com/' ) ;
console . log ( response . ok ) ;
console . log ( response . status ) ;
console . log ( response . statusText ) ;
console . log ( response . headers . raw ( ) ) ;
console . log ( response . headers . get ( 'content-type' ) ) ; ซึ่งแตกต่างจากเบราว์เซอร์คุณสามารถเข้าถึงส่วนหัวของ Set-Cookie Raw ด้วยตนเองโดยใช้ Headers.raw() นี่คือ API node-fetch เท่านั้น
import fetch from 'node-fetch' ;
const response = await fetch ( 'https://example.com' ) ;
// Returns an array of values, instead of a string of comma-separated values
console . log ( response . headers . raw ( ) [ 'set-cookie' ] ) ; import fetch , {
Blob ,
blobFrom ,
blobFromSync ,
File ,
fileFrom ,
fileFromSync ,
} from 'node-fetch'
const mimetype = 'text/plain'
const blob = fileFromSync ( './input.txt' , mimetype )
const url = 'https://httpbin.org/post'
const response = await fetch ( url , { method : 'POST' , body : blob } )
const data = await response . json ( )
console . log ( data )Node-Fetch มาพร้อมกับการใช้งาน FormData ที่สอดคล้องกับสเป็คสำหรับการโพสต์ payloads แบบหลายส่วน/ฟอร์มข้อมูล
import fetch , { FormData , File , fileFrom } from 'node-fetch'
const httpbin = 'https://httpbin.org/post'
const formData = new FormData ( )
const binary = new Uint8Array ( [ 97 , 98 , 99 ] )
const abc = new File ( [ binary ] , 'abc.txt' , { type : 'text/plain' } )
formData . set ( 'greeting' , 'Hello, world!' )
formData . set ( 'file-upload' , abc , 'new name.txt' )
const response = await fetch ( httpbin , { method : 'POST' , body : formData } )
const data = await response . json ( )
console . log ( data )หากคุณด้วยเหตุผลบางอย่างจำเป็นต้องโพสต์สตรีมที่มาจากสถานที่ใด ๆ โดยพลการคุณสามารถต่อท้ายหยดหรือรายการไฟล์ที่ดูเหมือนไฟล์
ข้อกำหนดขั้นต่ำคือมี:
Symbol.toStringTag getter หรือคุณสมบัติที่เป็น Blob หรือ Filestream() หรือเมธอด arrayBuffer() ที่ส่งคืน ArrayBuffer stream() จะต้องส่งคืนวัตถุ aterable ใด ๆ ตราบเท่าที่มันให้ Uint8Array (หรือบัฟเฟอร์) ดังนั้นสตรีมที่อ่านได้และสตรีม whatwg ทำงานได้ดี
formData . append ( 'upload' , {
[ Symbol . toStringTag ] : 'Blob' ,
size : 3 ,
* stream ( ) {
yield new Uint8Array ( [ 97 , 98 , 99 ] )
} ,
arrayBuffer ( ) {
return new Uint8Array ( [ 97 , 98 , 99 ] ) . buffer
}
} , 'abc.txt' ) คุณสามารถยกเลิกคำขอกับ AbortController การใช้งานที่แนะนำคือ abort-controller
ตัวอย่างของการกำหนดเวลาการร้องขอหลังจาก 150ms สามารถทำได้ดังต่อไปนี้:
import fetch , { AbortError } from 'node-fetch' ;
// AbortController was added in node v14.17.0 globally
const AbortController = globalThis . AbortController || await import ( 'abort-controller' )
const controller = new AbortController ( ) ;
const timeout = setTimeout ( ( ) => {
controller . abort ( ) ;
} , 150 ) ;
try {
const response = await fetch ( 'https://example.com' , { signal : controller . signal } ) ;
const data = await response . json ( ) ;
} catch ( error ) {
if ( error instanceof AbortError ) {
console . log ( 'request was aborted' ) ;
}
} finally {
clearTimeout ( timeout ) ;
}ดูกรณีทดสอบสำหรับตัวอย่างเพิ่มเติม
url สตริงที่แสดงถึง URL สำหรับการดึงข้อมูลoptions สำหรับคำขอ HTTP (s)Promise<Response>ทำการดึง HTTP (S)
url ควรเป็น URL ที่แน่นอนเช่น https://example.com/ URL ที่เกี่ยวข้องกับเส้นทาง ( /file/under/root ) หรือ URL ที่เกี่ยวข้องกับโปรโตคอล ( //can-be-http-or-https.com/ ) จะส่งผลให้ Promise ถูกปฏิเสธ
ค่าเริ่มต้นจะแสดงหลังจากแต่ละคีย์ตัวเลือก
{
// These properties are part of the Fetch Standard
method : 'GET' ,
headers : { } , // Request headers. format is the identical to that accepted by the Headers constructor (see below)
body : null , // Request body. can be null, or a Node.js Readable stream
redirect : 'follow' , // Set to `manual` to extract redirect headers, `error` to reject redirect
signal : null , // Pass an instance of AbortSignal to optionally abort requests
// The following properties are node-fetch extensions
follow : 20 , // maximum redirect count. 0 to not follow redirect
compress : true , // support gzip/deflate content encoding. false to disable
size : 0 , // maximum response body size in bytes. 0 to disable
agent : null , // http(s).Agent instance or function that returns an instance (see below)
highWaterMark : 16384 , // the maximum number of bytes to store in the internal buffer before ceasing to read from the underlying resource.
insecureHTTPParser : false // Use an insecure HTTP parser that accepts invalid HTTP headers when `true`.
} หากไม่มีการตั้งค่าค่าส่วนหัวคำขอต่อไปนี้จะถูกส่งโดยอัตโนมัติ:
| ส่วนหัว | ค่า |
|---|---|
Accept-Encoding | gzip, deflate, br (เมื่อ options.compress === true ) |
Accept | */* |
Content-Length | (คำนวณโดยอัตโนมัติถ้าเป็นไปได้) |
Host | (ข้อมูลโฮสต์และพอร์ตจากเป้าหมาย URI) |
Transfer-Encoding | chunked (เมื่อ req.body เป็นสตรีม) |
User-Agent | node-fetch |
หมายเหตุ: เมื่อ body เป็น Stream Content-Length จะไม่ถูกตั้งค่าโดยอัตโนมัติ
ตัวเลือก agent ช่วยให้คุณระบุตัวเลือกที่เกี่ยวข้องกับเครือข่ายซึ่งอยู่นอกขอบเขตของการดึงข้อมูลรวมถึงและไม่ จำกัด เฉพาะสิ่งต่อไปนี้:
ดู http.Agent สำหรับข้อมูลเพิ่มเติม
หากไม่ได้ระบุเอเจนต์จะใช้เอเจนต์เริ่มต้นที่จัดทำโดย node.js โปรดทราบว่าสิ่งนี้เปลี่ยนไปใน node.js 19 เพื่อให้มี keepalive จริงโดยค่าเริ่มต้น หากคุณต้องการเปิดใช้งาน keepalive ใน Node.js เวอร์ชันก่อนหน้าคุณสามารถแทนที่เอเจนต์ตามตัวอย่างรหัสต่อไปนี้
นอกจากนี้ตัวเลือก agent รับฟังก์ชั่นที่ส่งคืนอินสแตน http (S) .Agent สแตนซ์ที่ได้รับ URL ปัจจุบันซึ่งมีประโยชน์ในระหว่างการเปลี่ยนเส้นทางห่วงโซ่การเปลี่ยนเส้นทางข้ามโปรโตคอล HTTP และ HTTPS
import http from 'node:http' ;
import https from 'node:https' ;
const httpAgent = new http . Agent ( {
keepAlive : true
} ) ;
const httpsAgent = new https . Agent ( {
keepAlive : true
} ) ;
const options = {
agent : function ( _parsedURL ) {
if ( _parsedURL . protocol == 'http:' ) {
return httpAgent ;
} else {
return httpsAgent ;
}
}
} ; สตรีมบน node.js มีขนาดบัฟเฟอร์ภายในที่เล็กกว่า (16KB, AKA highWaterMark ) จากเบราว์เซอร์ฝั่งไคลเอ็นต์ (> 1MB, ไม่สอดคล้องกันในเบราว์เซอร์) ด้วยเหตุนี้เมื่อคุณเขียนแอพ isomorphic และใช้ res.clone() มันจะแขวนกับการตอบสนองขนาดใหญ่ในโหนด
วิธีที่แนะนำในการแก้ไขปัญหานี้คือการแก้ไขการตอบสนองที่โคลนในแบบคู่ขนาน:
import fetch from 'node-fetch' ;
const response = await fetch ( 'https://example.com' ) ;
const r1 = response . clone ( ) ;
const results = await Promise . all ( [ response . json ( ) , r1 . text ( ) ] ) ;
console . log ( results [ 0 ] ) ;
console . log ( results [ 1 ] ) ; หากด้วยเหตุผลบางอย่างที่คุณไม่ชอบโซลูชันด้านบนเนื่องจาก 3.x คุณสามารถแก้ไขตัวเลือก highWaterMark ได้:
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 ) ; ผ่านไปยังตัวเลือก insecureHTTPParser บน http (s). request ดู http.request สำหรับข้อมูลเพิ่มเติม
ตัวเลือก redirect: 'manual' สำหรับการดึงข้อมูลโหนดนั้นแตกต่างจากเบราว์เซอร์และข้อกำหนดซึ่งส่งผลให้เกิดการตอบสนองแบบกรองแบบทึบแสง Node-Fetch ช่วยให้คุณได้รับการตอบกลับแบบกรองพื้นฐานแทน
import fetch from 'node-fetch' ;
const response = await fetch ( 'https://httpbin.org/status/301' , { redirect : 'manual' } ) ;
if ( response . status === 301 || response . status === 302 ) {
const locationURL = new URL ( response . headers . get ( 'location' ) , response . url ) ;
const response2 = await fetch ( locationURL , { redirect : 'manual' } ) ;
console . dir ( response2 ) ;
}คำขอ HTTP (S) ที่มีข้อมูลเกี่ยวกับ URL, วิธี, ส่วนหัวและร่างกาย คลาสนี้ใช้อินเทอร์เฟซร่างกาย
เนื่องจากลักษณะของ node.js คุณสมบัติต่อไปนี้จะไม่ถูกนำไปใช้ในขณะนี้:
typedestinationmodecredentialscacheintegritykeepaliveมีคุณสมบัติส่วนขยายการดึงโหนดต่อไปนี้:
followcompresscounteragenthighWaterMarkดูตัวเลือกสำหรับความหมายที่แน่นอนของส่วนขยายเหล่านี้
(ตามข้อกำหนดตามข้อกำหนด)
input สตริงที่แสดงถึง URL หรือ Request อื่น (ซึ่งจะถูกโคลน)options สำหรับคำขอ HTTP (s) สร้างวัตถุ Request ใหม่ ตัวสร้างนั้นเหมือนกับในเบราว์เซอร์
ในกรณีส่วนใหญ่ fetch(url, options) ง่ายกว่าการสร้างวัตถุ Request
การตอบสนอง HTTP (S) คลาสนี้ใช้อินเทอร์เฟซร่างกาย
คุณสมบัติต่อไปนี้จะไม่ถูกนำไปใช้ในการดึงโหนดในขณะนี้:
trailer(ตามข้อกำหนดตามข้อกำหนด)
body String หรือสตรีม Readableoptions A ResponseInit Optionary สร้างวัตถุ Response ใหม่ ตัวสร้างนั้นเหมือนกับในเบราว์เซอร์
เนื่องจาก node.js ไม่ได้ใช้พนักงานบริการ (ซึ่งคลาสนี้ได้รับการออกแบบ) จึงไม่ค่อยมีการสร้าง Response โดยตรง
(ตามข้อกำหนดตามข้อกำหนด)
ทรัพย์สินความสะดวกสบายเป็นตัวแทนหากคำขอสิ้นสุดลงตามปกติ จะประเมินเป็นจริงหากสถานะการตอบสนองมากกว่าหรือเท่ากับ 200 แต่เล็กกว่า 300
(ตามข้อกำหนดตามข้อกำหนด)
ทรัพย์สินความสะดวกสบายเป็นตัวแทนหากคำขอถูกเปลี่ยนเส้นทางอย่างน้อยหนึ่งครั้ง จะประเมินเป็นจริงหากตัวนับการเปลี่ยนเส้นทางภายในมากกว่า 0
(การเบี่ยงเบนจากสเป็ค)
ทรัพย์สินความสะดวกสบายเป็นตัวแทนของประเภทการตอบสนอง Node-Fetch รองรับ 'default' และ 'error' เท่านั้นและไม่ได้ใช้ประโยชน์จากการตอบกลับที่ผ่านการกรอง
คลาสนี้อนุญาตให้จัดการและวนซ้ำชุดส่วนหัว HTTP วิธีการทั้งหมดที่ระบุในมาตรฐานการดึงข้อมูลจะถูกนำมาใช้
(ตามข้อกำหนดตามข้อกำหนด)
init อาร์กิวเมนต์ตัวเลือกเพื่อเติมวัตถุ Headers ล่วงหน้า สร้างวัตถุ Headers ใหม่ init สามารถเป็น null , วัตถุ Headers , วัตถุแผนที่ค่าคีย์หรือวัตถุที่ทำซ้ำได้
// Example adapted from https://fetch.spec.whatwg.org/#example-headers-class
import { Headers } from 'node-fetch' ;
const meta = {
'Content-Type' : 'text/xml'
} ;
const headers = new Headers ( meta ) ;
// The above is equivalent to
const meta = [ [ 'Content-Type' , 'text/xml' ] ] ;
const headers = new Headers ( meta ) ;
// You can in fact use any iterable objects, like a Map or even another Headers
const meta = new Map ( ) ;
meta . set ( 'Content-Type' , 'text/xml' ) ;
const headers = new Headers ( meta ) ;
const copyOfHeaders = new Headers ( headers ) ; Body เป็นอินเทอร์เฟซนามธรรมพร้อมวิธีการที่ใช้กับทั้ง Request และคลาส Response
(การเบี่ยงเบนจากสเป็ค)
Readable js ข้อมูลถูกห่อหุ้มในวัตถุ Body โปรดทราบว่าในขณะที่มาตรฐานการดึงข้อมูลต้องการคุณสมบัติที่จะเป็น whatwg ReadableStream เสมอใน Node-Fetch มันเป็นสตรีม Node.js Readable
(ตามข้อกำหนดตามข้อกำหนด)
Booleanทรัพย์สินบูลีนสำหรับหากร่างกายนี้ถูกบริโภค ตามสเป็คไม่สามารถใช้ร่างกายที่บริโภคได้อีกครั้ง
fetch มาพร้อมกับวิธีการแยก multipart/form-data รวมถึง x-www-form-urlencoded BOFIES โดยใช้ .formData() ซึ่งมาจากแนวคิดที่ว่าผู้ปฏิบัติงานสามารถสกัดกั้นข้อความดังกล่าวก่อนที่จะส่งไปยังเซิร์ฟเวอร์เพื่อเปลี่ยนแปลง สิ่งนี้มีประโยชน์สำหรับทุกคนที่สร้างเซิร์ฟเวอร์เพื่อให้คุณสามารถใช้เพื่อแยกวิเคราะห์และบริโภคน้ำหนักบรรทุก
import http from 'node:http'
import { Response } from 'node-fetch'
http . createServer ( async function ( req , res ) {
const formData = await new Response ( req , {
headers : req . headers // Pass along the boundary value
} ) . formData ( )
const allFields = [ ... formData ]
const file = formData . get ( 'uploaded-files' )
const arrayBuffer = await file . arrayBuffer ( )
const text = await file . text ( )
const whatwgReadableStream = file . stream ( )
// other was to consume the request could be to do:
const json = await new Response ( req ) . json ( )
const text = await new Response ( req ) . text ( )
const arrayBuffer = await new Response ( req ) . arrayBuffer ( )
const blob = await new Response ( req , {
headers : req . headers // So that `type` inherits `Content-Type`
} . blob ( )
} )(ส่วนขยายของ Node-Fetch)
ข้อผิดพลาดในการดำเนินงานในกระบวนการดึงข้อมูล ดูข้อผิดพลาด handling.md สำหรับข้อมูลเพิ่มเติม
(ส่วนขยายของ Node-Fetch)
ข้อผิดพลาดที่เกิดขึ้นเมื่อคำขอถูกยกเลิกเพื่อตอบสนองต่อเหตุการณ์ abort ของ AbortSignal มันมีคุณสมบัติ name ของ AbortError ดูข้อผิดพลาด handling.md สำหรับข้อมูลเพิ่มเติม
เนื่องจากประเภท 3.x ถูกรวมเข้ากับ node-fetch ดังนั้นคุณไม่จำเป็นต้องติดตั้งแพ็คเกจเพิ่มเติมใด ๆ
สำหรับเวอร์ชันเก่าโปรดใช้คำจำกัดความประเภทจากแน่นอนว่า:
npm install --save-dev @types/[email protected]ขอบคุณ GitHub/Fetch สำหรับการอ้างอิงการใช้งานที่แข็งแกร่ง
![]() | ![]() | ![]() | ![]() | ![]() |
|---|---|---|---|---|
| เดวิดแฟรงค์ | Jimmy Wärting | Antoni Kepinski | Richie Bendall | Gregor Martynus |
มิกซ์