Node.JS Revolution: Farewell to Axios and Fetch API in Version 17.5.0

Bernardo Leite JavaScript, Node.js Leave a Comment

Every self-respecting student of programming has already needed to use libraries (the famous “lib”) in the development of their applications. Examples include the Axios and Fetch libs, which are widely used on the front-end and back-end to consume external APIs.

Recently, Node.JS v17.5 underwent two changes that divided opinions, so let’s explain how these changes impact the issues of effectiveness and agility in code production. Additionally, I will also discuss the pros and cons of this update.

Understanding the Different Libraries

To start, let’s remember how the Axios, Fetch, and Node Fetch libs work.

With an excellent reputation in the community, Axios is a popular Javascript lib for making HTTP requests from Node.JS or XMLHttpRequests through the browser. It also supports the Promise API, which is native to JS ES6, transforms data into JSON, and transforms and intercepts request and response data (request/response). In addition to all this, on the client-side, it supports XSRF protection.

In the Javascript language, Axios is an alternative to the fetch() method because it can do an automatic analysis of JSON and works very well in partnership with Express. While Axios sends the web request, Express responds to these requests.

On the other hand, Axios is an external package that you need to install in your project, unlike Fetch, which is already built into the browser and, therefore, lighter than Axios.

For those who are on the Node-Fetch team, you know that it will be easy and light to install the module. With a one simple npm install node-fetch you have the Fetch resources in a much more direct way and without having to implement the XMLHttpRequest. That is, it is an attraction for those who like a cleaner and more organized code.

Here’s an example of Node-Fetch using async and await asynchronous functions.

const express = require(“express”);
const fetch = require(“node-fetch”);
const app = express();

app.get(‘/’, async function(req, res){
const response = await fetch(‘https://dog.ceo/api/breeds/list/all’)
	const app = await response.json()
	console.log(app)
})
app.listen(3000);
module.export = app

Note that in the application, you may need frameworks and maybe other libraries in addition to Node-Fetch. This can make the application heavy with so many dependencies.

Simply put, new to the Node.JS ecosystem is a native way to implement the Fetch API without lib or installing external modules.

Axios and Fetch

You will now have the fetch, Request, Response, and the Headers as globals. Then, just consume the Fetch API! Here is a very simple example.

Now, all you need is to send the request and the external API will respond back in a much simpler way.

Conclusion

There are many reasons for you to consider using fetch() in Node.JS because, in addition to being excellent for performing simple requests, we don’t need to worry about possible changes in external libs and consequently compromising our applications.

It is still important to remember that native Fetch is not yet available for the LTS version, which means that there may be versioning issues or instability in the latest version. However, it is nothing that compromises the new implementation.

Native Fetch also references Undici, which guarantees a significant improvement in latency and file transfer rate.

For more on Node.JS and other popular technologies, take a look at our Development Blog. All posts are written by other expert Keyhole consultants.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments