Let's explore Axios JS and its usage cases with Node
While building a Node.js application or microservice is pretty common you might end up needing to consume a third-party API, or just doing HTTP calls in general, and if that's the case Axios got you covered.
Axios is a promise based HTTP client for the browser and Node.js
Axios provides us an elegant and modern way to handle HTTP requests from Node using promises, to avoid old practices like having to rely on callbacks or deprecated modules such as Request.
The pros of using Axios are:
native ES6 Promise implementation.
It can be extended using modules.
It comes with TypeScript typing out of the box.
Requests can be canceled using Cancel Tokens.
Able to handle multiple concurrent requests.
Automatic transforms for JSON data
Intercept request and response
What you will learn#
By the end of this tutorial you will have a good grasp on the following:
How to do HTTP requests from Node.js using Axios
Handling Axios promises using async-await.
How to handle Axios error elegantly.
A brief introduction to Axio's popular attachable modules, like retry and mock adapter.
Prerequisites - What You Need To Know#
If you have ever used before Node.js you are good to go, this is an entry-level article, but here are some interesting articles that might enhance your grasp on the subject.
Step 1: install Axios#
Axios is available through most of the major Node package registry
Step 2: A basic HTTP GET request using Axios#
To import Axios in our node app we do:
Lets now use the library to do our first HTTP GET request, also to take advantage of Axios native promises using await we will abstract it using an async function:
jsonPlaceholder is a fun little toy API server, which is great for this exercise, the resulting response object will be something like this, lets put our emphasis in the data property, which contains the response data sent by the server:
Step 3: Additional HTTP [POST / PUT / DELETE] requests using Axios#
axios.post(url, data , options) , axios.put(url, data, options ) and axios.delete(url)
Step 4: Using custom headers and custom configuration in our Axios HTTP requests#
Often you will have the need to set custom request headers, or setting a custom request timeout, we can achieve this through the Axios request config option
Step 5: Dive into the hierarchy of error handling of Axios requests#
To properly check for the Axios errors, to get the most amount of information, we need to check in the proper order:
Error response from the server: our request made it to the server, and we got a response, with a status code different than 200.
The request was made, but we got no response: this could be due to a timeout issue, or a critical error in the server where no response was provided back.
The request couldn't be made: this could be due to an internal exception, or due to network issue, or socket issue.
In the following code we showcase the proper way to filter through these cases, using a catch block:
Step 6: Some fun Axios plugins to enhance its functionality#
The first one we are going to showcase is, axios-retry, and as its name states is a retry plugin for Axios, let us retry failed requests, and set retry conditions and timeouts for these requests:
To install axios-retry using NPM we do
Last but not least, let's try out the Axios mock adapter
To install axios-mock-adapter using NPM we do:
Axios mock adapter allows us to easily create a mock server and attach it to our Axios instance, this is ideal for a controlled testing environment where you want to black-box your application
We've been through a lot, within this short post, but with these tools, you should be able to do the basic HTTP operations with Axios.
Have fun doing HTTP requests to an API, or crawling a website (while being responsible!) or just pinging your services through to know if they are alive.
Have fun and keep coding!
Check out the documentation of the modules we used in this post:
Getting Help (and Feedback)#
If you have any questions - or want feedback on your post - come join our community Discord. See you there!