Saturday, March 2, 2024

Learn how to use the Fetch API in Node.js, Deno, and Bun — SitePoint

Must read


On this article, we’ll have a look at how you can use the Fetch API with Node.js, Deno, and Bun.

Desk of Contents

Fetch API vs XMLHttpRequest

Fetching knowledge through an HTTP request is prime internet software exercise. You could have made such calls within the browser, however the Fetch API is natively supported in Node.js, Deno, and Bun.

In a browser, you would possibly request info from a server so you’ll be able to show it with out a full display refresh. That is usually referred to as an Ajax request or a single web page software (SPA). Between 1999 and 2015, XMLHttpRequest was the one possibility — and stays so if you wish to present file add progress. XMLHttpRequest is a reasonably clunky callback-based API, however it permits fine-grained management and, regardless of the identify, it’ll deal with responses in codecs aside from XML — corresponding to textual content, binary, JSON, and HTML.

Browsers have applied the Fetch API from 2015. It’s a less complicated, simpler, extra constant, promise-based various to XMLHttpRequest.

Your server-side code may need to make HTTP requests — usually to name APIs on different servers. From their first launch, each the Deno and Bun runtimes usefully replicated the browser’s Fetch API in order that comparable code might run on each the shopper and server. Node.js required a third-party module corresponding to node-fetch or axios till February 2022, when model 18 added the usual Fetch API. It’s nonetheless thought-about experimental, however now you can use fetch() all over the place with an identical code generally.

A Fundamental Fetch Instance

This easy instance fetches response knowledge from a URI:

const response = await fetch('https://instance.com/knowledge.json');

The fetch() name returns a promise which resolves with a Response object offering details about the end result. You’ll be able to parse the HTTP response physique right into a JavaScript object utilizing the promise-based .json() technique:

const knowledge = await response.json();



Consumer-side vs Server-side Fetch

The API could also be an identical throughout platforms, however browsers implement restrictions when making client-side fetch() requests:

  • Cross-origin useful resource sharing (CORS)

    Consumer-side JavaScript can solely talk with API endpoints inside its personal area. A script loaded from https://domainA.com/js/fundamental.js can name any service at https://domainA.com/, corresponding to https://domainA.com/api/ or https://domainA.com/knowledge/.

    It’s not possible to name a service on https://domainB.com/ — until that server permits entry by setting an HTTP Entry-Management-Enable-Origin header.

  • Content material Safety Coverage (CSP)

    Your websites/apps can set a Content material-Safety-Coverage HTTP header or meta tag to regulate permitted property in a web page. It might probably forestall unintended or malicious injection of scripts, iframes, fonts, pictures, movies, and so forth. For instance, setting default-src 'self' stops fetch() requesting knowledge outdoors its personal area (XMLHttpRequest, WebSocket, server-sent occasions, and beacons are additionally restricted).

Server-side Fetch API calls in Node.js, Deno, and Bun have fewer restrictions, and you’ll request knowledge from any server. That mentioned, third-party APIs could:

  • require some kind of authentication or authorization utilizing keys or OAuth
  • have most request thresholds, corresponding to no a couple of name per minute, or
  • make a industrial cost for entry

You need to use server-side fetch() calls to proxy client-side requests so you’ll be able to keep away from CORS and CSP points. That mentioned, keep in mind to be a conscientious internet citizen and don’t bombard companies with 1000’s of requests that would take them down!

Customized Fetch Requests

The instance above requests knowledge from the URI https://instance.com/knowledge.json. Under the floor, JavaScript creates a Request object, which represents the complete particulars of that request corresponding to the strategy, headers, physique, and extra.

fetch() accepts two arguments:

  • the useful resource – a string or URL object, and
  • an non-compulsory choices parameter with additional request settings

For instance:

const response = await fetch('https://instance.com/knowledge.json', {
   technique: 'GET',
   credentials: 'omit',
   redirect: 'error',
   precedence: 'excessive'
});

The choices object can set following properties in Node.js or client-side code:

property values
technique GET (the default), POST, PUT, PATCH, DELETE, or HEAD
headers a string or Headers object
physique generally is a string, JSON, blob, and so forth.
mode same-origin, no-cors, or cors
credentials omit, same-origin, or embody cookies and HTTP authentication headers
redirect comply with, error, or guide dealing with of redirects
referrer the referring URL
integrity subresource integrity hash
sign an AbortSignal object to cancel the request

Optionally, you’ll be able to create a Request object and move it to fetch(). This can be sensible for those who can outline API endpoints upfront or need to ship a collection comparable requests:

const request = new Request('https://instance.com/api/', {
  technique: 'POST',
  physique: '{"a": 1, "b": 2, "c": 3}',
  credentials: 'omit'
});

console.log(`fetching ${ request.url }`);
const response = await fetch(request);

Dealing with HTTP Headers

You’ll be able to manipulate and study HTTP headers within the request and response utilizing a Headers object. The API might be acquainted for those who’ve used JavaScript Maps:


const headers = new Headers({
  'Content material-Sort': 'textual content/plain',
});


headers.append('Authorization', 'Fundamental abc123');


headers.set('Content material-Sort', 'software/json');


const sort = headers.get('Content material-Sort');


if (headers.has('Authorization')) {

   
   headers.delete('Authorization');

}


headers.forEach((worth, identify) => {
  console.log(`${ identify }: ${ worth }`);
});


const response = await fetch('https://instance.com/knowledge.json', {
   technique: 'GET',
   headers
});


response.headers.forEach((worth, identify) => {
  console.log(`${ identify }: ${ worth }`);
});

Fetch Promise Resolve and Reject

You would possibly presume a fetch() promise will reject when an endpoint returns a 404 Not Discovered or comparable server error. It doesn’t! The promise will resolve, as a result of that decision was profitable — even when the end result wasn’t what you anticipated.

A fetch() promise solely rejects when:

  • you make an invalid request — corresponding to fetch('httttps://!invalidURL/');
  • you abort the fetch() request, or
  • there’s a community error, corresponding to a connection failure

Analyzing Fetch Responses

Profitable fetch() calls return a Response object containing details about the state and returned knowledge. The properties are:

property description
okay true if the response was profitable
standing the HTTP standing code, corresponding to 200 for fulfillment
statusText the HTTP standing textual content, corresponding to OK for a 200 code
url the URL
redirected true if the request was redirected
sort the response sort: fundamental, cors, error, opaque, or opaqueredirect
headers the response Headers object
physique a ReadableStream of physique content material (or null)
bodyUsed true if the physique has been learn

The next Response object strategies all return a promise, so it is best to use await or .then blocks:

technique description
textual content() returns the physique as a string
json() parses the physique to a JavaScript object
arrayBuffer() returns the physique as an ArrayBuffer
blob() returns the physique as a Blob
formData() returns the physique as a FormData object of key/worth pairs
clone() clones the response, usually so you’ll be able to parse the physique in several methods

const response = await fetch('https://instance.com/knowledge.json');


if ( response.okay && response.headers.get('Content material-Sort') === 'software/json') {

   
   const obj = await response.json();

}

Aborting Fetch Requests

Node.js received’t day trip a fetch() request; it might run eternally! Browsers also can wait between one and 5 minutes. It’s best to abort fetch() below regular circumstances the place you’re anticipating a fairly fast response.

The next instance makes use of an AbortController object, which passes a sign property to the second fetch() parameter. A timeout runs the .abort() technique if fetch doesn’t full inside 5 seconds:


const
  controller = new AbortController(),
  sign = controller.sign,
  timeout = setTimeout(() => controller.abort(), 5000);

attempt {

  const response = await fetch('https://instance.com/slowrequest/', { sign });

  clearTimeout(timeout);

  console.log( response.okay );

}
catch (err) {

  
  console.log(err);

}

Node.js, Deno, Bun, and most browsers launched since mid-2022 additionally help AbortSignal. This presents a less complicated timeout() technique so that you don’t need to handle your individual timers:

attempt {

  
  const response = await fetch('https://instance.com/slowrequest/', {
    sign: AbortSignal.timeout( 5000 ),
  });

  console.log( response.okay );

}
catch (err) {

  
  console.log(err);

}

Efficient Fetches

Like all asynchronous, promise-based operation, it is best to solely make fetch() calls in collection when the enter of a name depends upon the output of a earlier one. The next code doesn’t carry out in addition to it might as a result of every API name should anticipate the earlier one to resolve or reject. If every response takes one second, it’ll take a complete of three seconds to finish:


const response1 = await fetch('https://example1.com/api/');
const response2 = await fetch('https://example2.com/api/');
const response3 = await fetch('https://example3.com/api/');

The Promise.allSettled() technique runs guarantees concurrently and fulfills when all have resolved or rejected. This code completes on the pace of the slowest response. It is going to be thrice sooner:

const knowledge = await Promise.allSettled(
  [
    'https://example1.com/api/',
    'https://example2.com/api/',
    'https://example3.com/api/'
  ].map(url => fetch( url ))
);

knowledge returns an array of objects the place:

  • every has a standing property string of "fullfilled" or "rejected"
  • if resolved, a worth property returns the fetch() response
  • if rejected, a motive property returns the error

Abstract

Except you’re utilizing a legacy model of Node.js (17 or under), the Fetch API is accessible in JavaScript on each the server and shopper. It’s versatile, simple to make use of, and constant throughout all runtimes. A 3rd-party module ought to solely be obligatory for those who require extra superior performance corresponding to caching, retries, or file dealing with.





Supply hyperlink

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article