Node.js: this is how the new Fetch API works

August 05, 2022

Tags: Technologies, Tech Trends

nodejs

 

Node.js is a Javascript interpreter that works on the server side and completely changes the way it should work. It uses a non-blocking and event-driven model of operations, which makes it lightweight and efficient. Despite being listed as a framework, Node.js is a "Java Runtime Environment". A Java Runtime Environment (JRE) is software designed to run other software.

 

Node.js users may worry about the process crashing as it doesn't exist. Almost no function in Node.js performs I/O directly, so the process never blocks. Therefore, it is very conducive to developing scalable systems in Node.js.

 

Fetch arrives: the new Node.Js API

 

The Fetch API is one of the most anticipated by Node.Js users. On February 1, 2022, the Node.js core team merged a pull request and added the Fetch API to Node.js. Although this API has been used for a long time, it is hardly integrated into Node.Js, since 2015 it is the successor of XMLHttpRequest and has become the most used to make asynchronous calls in web applications.

 

How to use Fetch in Node.Js

 

The Fetch API is a high-level function and takes a URL to produce a promise that resolves to the response, as follows:

 

fetch("http://example.com/api/endpoint")
  .then((response) => {
    // Do something with response
  })
  .catch(function (error) {
    console.log("Unable to fetch -", err);
  });

 

You can also change the way the fetching process is done by adding an optional object after the URL.

 

Instalation

 

npm install node-fetch

 

Configuring the modules

 

ES module

 

import fetch from 'node-fetch';

 

CommonJS

 

npm install node-fetch@2

 

Global access

 

To use Fetch without importing it, you can use:

 

// fetch-polyfill.js
import fetch, {
  Blob,
  blobFrom,
  blobFromSync,
  files,
  fileFrom,
  fileFromSync,
  FormData,
  headers,
  request,
  Reply,
} from 'node-fetch'

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

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

// ...

 

Benefits of using the Fetch API in Node.Js

 

Extra search pack is removed

 

Having Fetch built into Node.js means the end of packages like node-fetch, got, cross-fetch and many others that were created for the same use. The developer does not have to install npm before doing networking on Node. The native Fetch API will make HTTP fetching in node environments feel much more seamless and natural.

 

Multi platform

 

If a developer already has experience using the Fetch API, they will have no problem using the Node.js integration. Instead, you will work in a simple and intuitive environment, as opposed to using external packages to achieve the same functionality.

 

Quick implementation

 

Fetch is based on Undici, an HTTP client known for its speed, reliability, and compatibility with all Node.js specifications. This anticipates improved performance of the Fetch API.

 

In LogRocket they comment “Overall, it's exciting that Fetch is finally coming to Node.js core, as this has been a long time request from the developer community. It may take a while (around a year or two) for the Fetch API to be fully stable in Node.js, as there is still a lot to do to make it standards compliant. Many exciting updates are also expected in the near future, one of which includes the addition of HTTP/2 support to Undici and finally the Fetch API itself.”

 

We recommend you on video


 

Yes, I liked thiscontent.